How do I create an Emacs SQL buffer? - sql

I can connect manually by doing:
M-x sql-postgres
and type the User, Database, Host, Server
I'd like to do something like:
(connect-foo (user "me")(database "bar")(host "earth"))
Solution so far:
I grabbed some code from sql.el and changed it so I can use it non-interactively.
(defun sql-create-buffer (profile)
"derived from sql-product-interactive in sql.el which is"
"covered by GNU General Public License version 3."
(setq sql-user (cdr (assoc 'user profile))
sql-password (cdr (assoc 'password profile))
sql-server (cdr (assoc 'server profile))
sql-database (cdr (assoc 'database profile)))
(setq product 'postgres) ;;(cdr (assoc 'product profile)))
(when (sql-product-feature :sqli-connect product)
(if (comint-check-proc "*SQL*")
(pop-to-buffer "*SQL*")
;; Connect to database.
(message "Login...")
(funcall (sql-product-feature :sqli-connect product))
;; Set SQLi mode.
(setq sql-interactive-product product)
(setq sql-buffer (current-buffer))
(sql-interactive-mode)
;; All done.
(message "Login...done")
(pop-to-buffer sql-buffer))))
(setq bar-profile '(
(product . "postgres")
(user . "me")
(password . "")
(server . "earth")
(database . "bar")))
I don't know what to do with the 'product' parameter, so I left it hard coded for now.
It is 'postgres in sql.el.
(sql-create-buffer bar-profile)

It seems that package is not intended to be used non-interactively (probably to discourage writing login info into init files, which is a perfectly sound principle).
However, if you really want to avoid answering the questions, you can simply redefine the query function, like this:
(defun sql-get-login (&rest what)
(setq sql-user "me"
sql-password "secret"
sql-server "localhost"
sql-database "MyDB"))

Related

not able tochange voices festival i arch linux

whenever i tries to change voice in festival in arch linux it give me this error
#<CLOSURE n (let ((me voice_cmu_us_awb_cg)) (require "/usr/share/festival/voices/us/cmu_us_awb_cg/festvox/cmu_us_awb_cg") (if (eq me voice_cmu_us_awb_cg) (error (string-append "autoload: \"""/usr/share/festival/voices/us/cmu_us_awb_cg/festvox/cmu_us_awb_cg" ".scm\" does not define " (quote voice_cmu_us_awb_cg)))) (apply voice_cmu_us_awb_cg n))>
can any one help me in this

Trouble with implementing a while loop in an interpreter in Racket

I am very new to working with Racket. I need my interpreter to correctly identify the following statement and execute the "while" loop.
(= x 10)
(while (> x 0) ((print x)(= x (- x 1))))))
Currently, my implementation of a while loop is throwing an error I don't entirely understand.
My partial implementation is as follows:
(define interp
(lambda (stmt myEnv)
(cond
((eqv? (car stmt) 'print) (begin
(display "\n")
(display (exp myEnv (cadr stmt)))
myEnv))
((eqv? (car stmt) '=) (extend-env myEnv (cadr stmt)(exp myEnv (caddr stmt))))
((eqv? (car stmt) 'while) (while myEnv (cadr stmt)))
(define (while cond body)
(when (cond)
(display "x")
(while cond body)))
I'm getting an error that says:
application: not a procedure;
expected a procedure that can be applied to arguments
given: '(extend-env x 10 (empty-env))
Which leads me to believe I am fatally misunderstanding a key component of how Racket processes functions. Could someone explain what my misstep may be, and how I could go about fixing this definition? A large portion of this skeleton code has been provided for me, so if possible I would like to get the while definition working without modifying anything other than the while definition and the line where I am passing the variables and calling the while definition.

Racket equivalents of python's __repr__ and __str__?

In python objects, overriding the methods __repr__ and __str__ of an object allows one to provide "unambiguous" and "human-readable" representations of the object, respectively. How does one achieve similar behavior in Racket?
I came across the printable<%> interface here, which seems like it should be usable for this purpose, but I haven't been able to get it to work quite as I expect it to. Building on the standard "fish" example from the docs:
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print "Print called!"))
(define/public (custom-write port)
(print "Write called!"))
(define/public (custom-display port)
(print "Display called!"))))
This is the output I get:
> (define f (new fish% [size 10]))
> f
"Display called!""Display called!""Print called!"
> (print f)
"Write called!""Print called!"
> (display f)
"Display called!""Display called!"
> (write f)
"Write called!""Write called!"
>
So the question is threefold:
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
What should the methods custom-print, custom-write, and custom-display evaluate to? Should they simply return a string, or should they actually entail the side effect of printing, writing, or displaying, as the case may be? E.g. should the custom-write method invoke the write function internally?
Is this the right construct to use for this purpose at all? If not, is there one / what is it?
As for
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
You have accidently used print in write, so writing the value, will first print the value.
(define/public (custom-write port)
(print "Write called!"))
A similar problem is present in display.
Also remember to print/write/display to the proper port.
Try
#lang racket
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print (~a "Print " current-size "\n") port))
(define/public (custom-write port)
(write (~a "Write " current-size "\n") port))
(define/public (custom-display port)
(display (~a "Display " current-size "\n") port))))
In the repl you will see:
> (define f (new fish% [size 10]))
> f
"Print 10\n"
> (display f)
Display 10
> (write f)
"Write 10\n"
Another answer has already helped you find the problem in your code—you need to use the port given as an argument, not the implicit (current-output-port)—but the explanation isn't quite right. To tackle your questions in reverse order:
Is this the right construct to use for this purpose at all? If not, is there one / what is it?
Yes, printable<%> is the right construct to use to customize printing of a class-based object. More generally, a struct type that is not a class can customize printing for instance through the gen:custom-write generic interface or the low-level prop:custom-write struct type property, which is used to implement printable<%>.
What should the methods custom-print, custom-write, and custom-display evaluate to? Should they simply return a string, or should they actually entail the side effect of printing, writing, or displaying, as the case may be? E.g. should the custom-write method invoke the write function internally?
The methods should actually do the side-effect of performing IO on the port they are given as an argument. They should use the corresponding function (e.g. write for custom-write, print for custom-print) internally for recursively printing/writing/displaying values in fields. On the other hand, when directly emitting particular characters, they should generally use functions like write-char, write-string, or printf. The docs for gen:custom-write give an example of a tuple datatype that prints as <1, 2, "a">: it uses write-string for the angle-brackets and commas, but recursive print/write/display for the elements of the tuple.
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
This is the most involved part of your question. Printing in Racket is customizable through several hooks: for a few examples, see current-print, port-write-handler, global-port-print-handler, and make-tentative-pretty-print-output-port. Many of these customization hooks use intermediate ports in the process of producing output.
One thing that is not a part of the explanation is the fact that you used print in your implementation, particularly as print is bound to the normal Racket function by lexical scope, not to a method of your object.
As an illustration, consider the following adaptation of your example, which reports to the (current-output-port) the identity of the port given as an argument to the method:
#lang racket
(define report
(let ([next-id 0]
[id-cache (make-hash)])
(λ (op port)
(printf "~a ~a ~v\n"
op
(hash-ref id-cache
port
(λ ()
(define id next-id)
(hash-set! id-cache port id)
(set! next-id (add1 next-id))
id))
port))))
(define fish%
(class* object% (printable<%>)
(super-new)
;; implement printable interface
(define/public (custom-print port quoting-depth)
(report "custom-print " port))
(define/public (custom-write port)
(report "custom-write " port))
(define/public (custom-display port)
(report "custom-display" port))))
(define f (new fish%))
f
(print f)
(newline)
(display f)
(newline)
(write f)
In DrRacket, this generates the output:
custom-display 0 #<output-port:null>
custom-display 1 #<output-port:null>
custom-print 2 #<printing-port>
custom-display 3 #<output-port:null>
custom-display 4 #<output-port:null>
custom-print 5 #<printing-port>
custom-display 6 #<output-port:null>
custom-display 7 #<printing-port>
custom-display 8 #<output-port:null>
custom-write 9 #<printing-port>
while at the command line, the output is:
$ racket demo.rkt
custom-write 0 #<output-port:null>
custom-print 1 #<output-port:redirect>
custom-write 2 #<output-port:null>
custom-print 3 #<output-port:redirect>
custom-display 4 #<output-port:null>
custom-display 5 #<output-port:redirect>
custom-write 6 #<output-port:null>
custom-write 7 #<output-port:redirect>

How do I do basic authentication in Emacs Lisp?

I'm trying to authenticate to an API using basic authentication. It seems like the code below should work, but it's breaking when I run it in the scratch buffer after running it with C-x C-e (says: debugger entered--Lisp error: (wrong-type-argument characterp)).
My code is below. If anyone knows what I need to change to make this work, please let me know! There are basically no complete Emacs Lisp basic authentication examples I could find online, so this would be a huge help.
(setq ztoken "areallyfaketokenrandomtokenwithfakechars")
(setq zsite "https://fake.zendesk.com/api/v2/users/1100000011/related.json")
(let ((url-request-method "GET")
(url-request-extra-headers '(("Content Type" . "application/json")
("Authorization" . ,(concat "Basic "
(base64-encode-string
(concat "joe#fake.com/token" ":" ztoken)))))))
(condition-case nil
(url-retrieve-synchronously
(format zsite))))
As lawlist mentions, your code uses a single-quote instead of a back-tick. Below is an alternative approach, that does not use the back-tick/comma. For more information on backquotes: https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html#Backquote
(let* ((url-request-method "GET")
(base64 (concat "Basic "
(base64-encode-string
(concat "joe#fake.com/token" ":" ztoken))))
(url-request-extra-headers (list (cons "Content Type" "application/json")
(cons "Authorization" base64))))
(condition-case nil
(url-retrieve-synchronously
(format zsite))))

Running and testing a property expressing a relationship between TAKE and APPEND

Basically, I need to write a what the title says, the only relationship I have been able to think of is if I take some number of elements from a list with TAKE and then take the not-as-important other half with CDR and then APPEND the two that I took to prove that it's the same as the original list.
(After long painful hours of building it), the proof seems to be fine (It compiles just fine), but for some reason it fails when run.
I am using Proofpad with Dracula, in case you need the information.
Here is the code:
(include-book "doublecheck" :dir :teachpacks)
(defproperty take-append-relationship-test ;not sure why this fails.
(xs :value (random-integer-list))
(iff (consp xs)
(let* ((x1 (take 1 xs))
(xs2 (cdr xs)))
(equal (append x1 xs2)
xs))))
Here is the error log I get.
By the simple :definition TAKE we reduce the conjecture to
Goal'
(COND ((CONSP XS)
(LET ((X1 (FIRST-N-AC 1 XS NIL)))
(EQUAL (APPEND X1 (CDR XS)) XS)))
((LET ((X1 (FIRST-N-AC 1 XS NIL)))
(EQUAL (APPEND X1 (CDR XS)) XS))
NIL)
(T T)).
This simplifies, using the :definition FIRST-N-AC, the :executable-
counterparts of BINARY-+, BINARY-APPEND, CONS, FIRST-N-AC and ZP, primitive
type reasoning and the :rewrite rules DEFAULT-CAR and DEFAULT-CDR,
to
Goal''
(IMPLIES (CONSP XS)
(EQUAL (APPEND (FIRST-N-AC 1 XS NIL) (CDR XS))
XS)).
The destructor terms (CAR XS) and (CDR XS) can be eliminated by using
CAR-CDR-ELIM to replace XS by (CONS XS1 XS2), (CAR XS) by XS1 and (CDR XS)
by XS2. This produces the following goal.
Goal'''
(IMPLIES (CONSP (CONS XS1 XS2))
(EQUAL (APPEND (FIRST-N-AC 1 (CONS XS1 XS2) NIL)
XS2)
(CONS XS1 XS2))).
This simplifies, using primitive type reasoning, to
Goal'4'
(EQUAL (APPEND (FIRST-N-AC 1 (CONS XS1 XS2) NIL)
XS2)
(CONS XS1 XS2)).
Normally we would attempt to prove Goal'4' by induction. However,
we prefer in this instance to focus on the original input conjecture
rather than this simplified special case. We therefore abandon our
previous work on this conjecture and reassign the name *1 to the original
conjecture. (See :DOC otf-flg.)
No induction schemes are suggested by *1. Consequently, the proof
attempt has failed.
Summary
Form: ( DEFTHM TAKE-APPEND-RELATIONSHIP-TEST ...)
Rules: ((:DEFINITION FIRST-N-AC)
(:DEFINITION IFF)
(:DEFINITION TAKE)
(:ELIM CAR-CDR-ELIM)
(:EXECUTABLE-COUNTERPART BINARY-+)
(:EXECUTABLE-COUNTERPART BINARY-APPEND)
(:EXECUTABLE-COUNTERPART CONS)
(:EXECUTABLE-COUNTERPART FIRST-N-AC)
(:EXECUTABLE-COUNTERPART ZP)
(:FAKE-RUNE-FOR-TYPE-SET NIL)
(:REWRITE DEFAULT-CAR)
(:REWRITE DEFAULT-CDR))
Time: 0.00 seconds (prove: 0.00, print: 0.00, other: 0.00)
Prover steps counted: 328
---
The key checkpoint goal, below, may help you to debug this failure.
See :DOC failure and see :DOC set-checkpoint-summary-limit.
---
*** Key checkpoint before reverting to proof by induction: ***
Goal''
(IMPLIES (CONSP XS)
(EQUAL (APPEND (FIRST-N-AC 1 XS NIL) (CDR XS))
XS))
ACL2 Error in ( DEFTHM TAKE-APPEND-RELATIONSHIP-TEST ...): See :DOC
failure.
******** FAILED ********
Could someone at least point me in the right direction?
I have read the error log and what I get is that there is some sort of redundancy.
I am not sure how to fix this at all.
If I do that (so like '(1 2 3 4) instead of (random-integer-list)), it throws an error:
ACL2 Error in TOP-LEVEL: One value, '(1 2 3 4 5), is being returned
where 2 values were expected. Note: This error occurred in the context
(MV-LET (XS STATE)
'(1 2 3 4 5)
(IF T
(MV-LET (STATE RESULT ASSIGNMENTS)
(EXPAND-VARS NIL (IFF # #))
(MV STATE RESULT (CONS # ASSIGNMENTS)))
(MV STATE 'WHERE-NOT-MATCHED NIL))).
(See :DOC set-iprint to be able to see elided values in this message.)
My setup:
Install Racket
Open it and run the following code:
(pound)lang racket
(require (planet cce/dracula:8:23/lang/dracula))
Restart Dr Racket. Click the "Choose Language" at the bottom of the window and choose ACL2 under Dracula.
From the Dracula menu, choose "Change ACL2 Executable Path..." and choose "run_acl2" (or "run_acl2.exe") which is located in the installation folder of proofpad (i.e. "C:\Program Files (x86)\Proof Pad", if you have a 64 bits PC)
download PSP++ and run it. Make sure to unzip it in the Proofpad installation directory, otherwise it will tell you it didn't find the things it needed.