Fork me on GitHub
#babashka
<
2024-04-12
>
RAJKUMAR00:04:04

Why I'm not able to use java.text DecimalFormat in babashka?

RAJKUMAR00:04:18

#!/usr/bin/env bb

(require '[babashka.cli :as cli])
(import  (java.time YearMonth))
(import  (java.text DecimalFormat))

(def cli-options {:month {:coerce :long}
                  :year  {:coerce :long}
                  :help  {:coerce :boolean}})

(def decimal-format (.DecimalFormat "00"))
(def last-hour "23:59:59")

(let [{:keys [month year]} (cli/parse-opts *command-line-args* {:spec cli-options})
      days-in-month    (.lengthOfMonth (YearMonth/of year month))
      two-letter-month (decimal-format/format month)
      formatted-date   (str year "-" two-letter-month "-" "01" " " last-hour)]
  (prn formatted-date))

RAJKUMAR00:04:26

This is my script

RAJKUMAR00:04:57

When I run I get this error

bb src/calc-month.clj --month 3 --year 2024
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Unable to resolve classname: java.text.DecimalFormat
Location: /Users/rnatarajan/Documents/Coding/ekata/calc-cpm/src/calc-month.clj:5:1

----- Context ------------------------------------------------------------------
1: #!/usr/bin/env bb
2:
3: (require '[babashka.cli :as cli])
4: (import  (java.time YearMonth))
5: (import  (java.text DecimalFormat))
   ^--- Unable to resolve classname: java.text.DecimalFormat
6:
7: (def cli-options {:month {:coerce :long}
8:                   :year  {:coerce :long}
9:                   :help  {:coerce :boolean}})
10:

----- Stack trace --------------------------------------------------------------
user - /Users/rnatarajan/Documents/Coding/ekata/calc-cpm/src/calc-month.clj:5:1

Sam Ferrell00:04:36

https://github.com/babashka/babashka/blob/master/src/babashka/impl/classes.clj I think this is a list of the java classes available in Babashka

seancorfield00:04:55

Babashka doesn't include all Java classes. Only three java.text classes are exposed: https://github.com/babashka/babashka/blob/master/src/babashka/impl/classes.clj#L412-L415

seancorfield00:04:35

You could just use format:

(~)-(!2002)-> rlwrap bb
Babashka v1.3.188 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (format "%02d" 12)
"12"
user=> (format "%02d" 2)
"02"
user=> (format "%02d" 10)
"10"
user=>

borkdude13:04:20

Yeah perhaps I could add this class since it came up before. I never ever used it myself though :)

pez07:04:01

Anyone know of some bb facilities for querying about git repo info?

pez07:04:09

I basically just want to know branch and git sha of the HEAD. But often when I roll something like that I discover that I am reinventing wheels. 😃

borkdude07:04:40

I usually just use babashka.process/sh or something and then parse the output

🙏 1
teodorlu08:04:08

https://github.com/clj-jgit/clj-jgit doesn’t work with babashka, right? Asking because there’s a Clerk notebook that uses clj-jgit: https://github.com/nextjournal/clerk-demo/blob/main/notebooks/git.clj

borkdude09:04:03

jgit certainly doesn't work with bb. note that even tools.deps dropped the dependency on jgit just to shell out to git instead

👍 1
borkdude09:04:38

and that dep fully works with bb :)

💯 1
pez09:04:28

Awesome. For my usecase p/sh was what I needed. But I’ll try to remember tools.gitlibs!

👍 1
rmxm14:04:32

just wondering, with babashka i get this nice exception with exact place in code that thrown, however why I catch and rethrow, I dont get it, is there some simple way to just try+catch, do some op and rethrow + get this nice marking?

borkdude14:04:23

if you catch with (catch Exception ^:sci/error e) then yes, I think

👍 1
Scott Lee15:04:34

Hi, first of all, many thanks to "The Dude"; love bb, it makes Clojure real and practical for a non-developer like me. A very lovely experience indeed. I have a question: I use babashka on Windows and it doesn't seem to be able to print Korean characters, printing question marks. Am I doing something?

borkdude15:04:22

Do you have a repro?

borkdude15:04:29

Welcome btw!

Scott Lee15:04:26

OMG OMG OMG am I talking to THE borkdude? I really appreciate all you have done. Thank you very much!!! Now as for the problem, it's as simple as (println "한글"), which prints ??? instead of 한글. Is bb supposed to print Asian character properly?

Scott Lee15:04:39

In DOS prompt and BB REPL respectively: echo "보크듀드 감사합니다!" "보크듀드 감사합니다!" user=> (println "보크듀드 감사합니다!") ?????? ????????! nil

borkdude15:04:02

ok let me try :)

borkdude15:04:16

$ rlwrap bb
Babashka v1.3.190-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (println "보크듀드 감사합니다!")
보크듀드 감사합니다!

borkdude15:04:32

this is on macos in iterm2

borkdude15:04:36

I can also try on Windows...

borkdude15:04:41

what terminal are you using?

Scott Lee15:04:57

on Windows 10.

borkdude15:04:13

have you also tried normal clojure? I'll double check

Scott Lee15:04:48

When using Calva on VS code, it prints properly in the output window. It's when I'm using cmd.exe when it doesn't print correctly.

borkdude15:04:43

I think it's probably a cmd.exe problem then

borkdude15:04:53

but let's try normal clojure

borkdude15:04:56

just a minute

borkdude15:04:58

yeah this isn't specific to bb:

C:\Users\borkdude>clj
Clojure 1.11.1
user=> (println "보크듀드 감사합니다!")
???? ?????!
nil

Scott Lee15:04:51

in lein repl, it work properly.

borkdude15:04:05

weird. try chcp 65001 in cmd.exe

borkdude15:04:17

this helps here locally

Scott Lee15:04:04

Active code page: 65001 C:\Users\urban>bb Babashka v1.3.189 REPL. Use :repl/quit or :repl/exit to quit the REPL. Clojure rocks, Bash reaches. user=> (println "한글") nil

Scott Lee15:04:48

Now instead of ?? it print

Scott Lee15:04:27

oh the characters aren't even printed properly here. Let me read the post and get back to you later!

Scott Lee15:04:44

It's an honor to talk to you!

😊 1
borkdude15:04:00

do you by any chance have wsl2 installed?

borkdude15:04:45

I don't know exactly what is the problem with bb + cmd.exe but it does work for me with wsl2 and mac iterm2

respatialized16:04:40

You may want to look into the details of how Windows encodes UTF-8 characters; frequently Windows applications default to the windows-1252 encoding for files which is primarily ASCII based and will fail on non-Latin character sets. Windows continues to rely on the presence of a https://en.wikipedia.org/wiki/Byte_order_mark to decode UTF-8 properly. I recently had to add a https://github.com/jimpil/clj-bom to my JVM Clojure project to deal with this issue on Windows. I suspect that a similar issue may be occurring here.

Scott Lee16:04:03

I don't have wsl2 installed, but it seems like the problem lies with cmd? It's not bb specific because the clj command example also prints ??? instead of Korean characters.

borkdude16:04:37

even with chcp 65001? sorry, my Windows knowledge is limited

respatialized16:04:41

you might also see if you have better luck with PowerShell than cmd

Scott Lee16:04:28

Yeah... no luck with chcp. May I should explore PowerShell or wls2!

Scott Lee16:04:27

Anyhow, it seems like the problem lies with something other than babashka. If I find anything useful, I'll come back and post.

🙏 1
Scott Lee15:04:35

This is my very first experience with this channel (and Slack) so if I'm not following any rules here by simply posting a question, please let me know!

Bailey Kocin17:04:44

Is there an alternative to clojure.core.memoize in Babashka? That library does not seem to be apart of Babashka so I was wondering if anyone uses anything else!

seancorfield17:04:44

How sophisticated a cache system do you need?

Bailey Kocin17:04:50

I am writing a babashka AWS lambda, and I wanted to avoid making it too complicated so a library that just worked would be nice, but honestly I just made a simple cache with an atom and it seems to be fine.... Its not too hard to follow

Bailey Kocin17:04:43

Might be a better way but like I just needed to cache a token

(defn memo!
  [f atom]
  (fn [& args]
    (if @atom
      @atom
      (reset! atom (apply f args)))))

(defn clear-memo!
  [atom]
  (fn []
    (reset! atom nil)))

borkdude17:04:25

looks good to me. (if x x ...) could be written as (or x ...) but this is just a minor nitpick

seancorfield17:04:42

And clojure.core/memoize isn't sufficient?

seancorfield17:04:59

i.e., the built-in function memoization

borkdude17:04:14

the built-in doesn't let you clear the atom

Bailey Kocin17:04:31

I need to clear the cache when I get a 401 back! Hence why I wanted to use the former I mentioned

seancorfield17:04:03

Ah, gotcha. Yeah, that would be a nice enhancement to the built-in one -- some magic set of arguments that caused the cache to be cleared for all invocations.

👀 1
suspect 1