Running procs from a list of procs

2024-08-24 Thread stbalbach
Great! I'll probably use the closure method only because I have not upgraded Nim in a long time and my version doesn't support std/tasks (2022?). This works for now, if more verbose: type MyTask = proc () {.closure.} proc hello(a: int) = echo a proc other(s: string) = e

Running procs from a list of procs

2024-08-24 Thread stbalbach
Thank you! You are correct, a few of my procs will need to pass a parameter. I tried something like this: import macros proc test1() = echo "Hello" proc test2(i: int) = echo "World " & $i for p in [test1, test2]: if astToStr(p) == "test2": p(42)

Running procs from a list of procs

2024-08-24 Thread stbalbach
Is it possible to make a list of proc names then loop through the list and run those procs. Perplexity.ai gave this answer which works: import tables proc test1() = echo "Hello" proc test2() = echo "World" let procTable = { "test1": test1

Best location to declare variables in a proc

2024-08-03 Thread stbalbach
Is it better to declare variables at the start of a proc, or closer to where needed? Thinking about loops proc testProc() = for i in 0..100: var j = "" j = $i Run There is overhead to create and destroy a variable 100 times. Yet, program best pr

Include a file dynamically

2024-04-11 Thread stbalbach
Is it possible to `include` a file named dynamically, such as a variable name? * * * Normally no, because Nim is statically compiled. I found workaround with a macro and compile-time define pragma. Posting for future reference. Add where the `include` statement will be generated: prog.nim nim

unhandled exception: index -1 not in 0 .. 12 [IndexDefect]

2024-01-14 Thread stbalbach
I'll keep looking. It won't be easy. A lot of code and files. Including tables.

unhandled exception: index -1 not in 0 .. 12 [IndexDefect]

2024-01-13 Thread stbalbach
Sometimes my program aborts with fatal.nim(49)sysFatal Error: unhandled exception: index -1 not in 0 .. 12 [IndexDefect] Run I am not handling range exceptions correctly, it is a two part question: how to trap exceptions of this type and, how to determine wh

cannot open: /dev/stderr

2023-12-16 Thread stbalbach
This appears resolved by setting the exception per above. Now looks like: # # showException # proc showException(msg: string) = try: stderr.write(msg) except Exception as e: discard e.msg # optionally log or email # #

cannot open: /dev/stderr

2023-12-13 Thread stbalbach
Excellent thanks for catching that. I did some research and found this: try: writeFile(filename, text) except Exception as e: echo e.msg Run I'm try it and see what happens, because it seems more robust for any type of Exception, future proof. If

cannot open: /dev/stderr

2023-12-12 Thread stbalbach
stderr.write(s) and writeFile("/dev/stderr", s) appear to use the same code, c_fputs(), which is a call to a C library function fputs(). They both can throw exceptions. I have never seen this before, in any application or language, where occasionally it fails to open stderr.

cannot open: /dev/stderr

2023-12-12 Thread stbalbach
This occasionally comes up .nim(855) writeFile Error: unhandled exception: cannot open: /dev/stderr [IOError] Run The only proc I have that uses writeFile # # > # # Write 'text\n' to 'filename', overwrite previous content. Clo

Detect replacement/binary characters?

2023-11-05 Thread stbalbach
Excellent, thank you.

Detect replacement/binary characters?

2023-11-05 Thread stbalbach
Replacement characters are those black diamonds with a ? in the middle. They are hard to detect. This has info how to do it in a bash script. Is there a way to do it in Nim? Thanks. The repl

downloading big files

2022-08-16 Thread stbalbach
I know curl is a standard but in my experience dealing with millions of random URLs from Wikipedia , curl will fail on websites where wget succeeded. You'd think something as basic as retrieving a web page would would work equally well but they have different internal assumptions and I find wget

downloading big files

2022-08-11 Thread stbalbach
Fool-proof method ;) proc runshell(command: string): string = let (output, _) = execCmdEx(command) return output proc download(url, target: string): string {.discardable} = return runshell("wget -q -O- \"" & url & "\" > " & target) dow

Why I left the Nim community

2022-05-10 Thread stbalbach
This is the first I heard there is a Nim fork. It's not a bad sign. Another language I often use is awk and that has dozens of varieties since its creation in the 1970s. The One True Awk is the version specified in `The AWK Programming Language`, by Aho, Kernighan, and Weinberger. For a long tim

Nested Tables

2021-10-30 Thread stbalbach
Yes wish tables were more intuitive. Maybe the correct term is associative array. It can sort of made easy with some init setup. This for a two key table, same idea for any table size or type type TwoKeyTable* = Table[string, Table[string, string]] proc initTwoKeyTabl

Question with nimpy

2021-06-29 Thread stbalbach
Given this, importing "numpy" works but "pandas" does not: import nimpy/nimpy let np = pyImport("numpy") let a = np.mean([1, 2, 3]) let f = np.sin(a) echo "numpy check" echo f let pd = pyImport("pandas") let df_temp = pd.DataFrame({"A": 1, "B": 2}

include a file variably

2021-06-27 Thread stbalbach
Thank you.

include a file variably

2021-06-27 Thread stbalbach
I would like to "include" a source file variably, such as based on an argument to the program via the command-line, or a compile-time switch. Is there a way to do so? I suspect the former is not possible but maybe the second. Example main.nim if : include myfile.nim

-d:release = awesome

2021-06-07 Thread stbalbach
I run bots with GNU parallel. The worker in Nim does its thing and exits, usually lasting no more than a few minutes, it might be 100s of thousands of times over many days to completion. This system has advantages, such as no issue with memory leaks caused by my convoluted code; the ability to h

How to escape colon in the '&' macro in strformat ?

2021-05-26 Thread stbalbach
Yes that is what I did, devel. Araq mentioned 1.6 and since that is > 1.4.6 I assumed it was in the devel branch, but sounds like revisions don't work that way. I couldn't tell since it didn't build.

How to escape colon in the '&' macro in strformat ?

2021-05-24 Thread stbalbach
This hack works (1.4.0) import strformat, re proc colon(): char = ':' echo &"""{re.replace("hello" & colon() & " world", re.re("hell"), "X")}""" Run FWIW (1.6 doesn't build for me via choosenim - will wait for stable to catch up)

How to escape colon in the '&' macro in strformat ?

2021-05-22 Thread stbalbach
The point of the curly brackets is to embed Nim code (not to "embed variables"), it can be anything. I happen to have Nim code that has quote marks in it, but it won't compile for anyone here. Since this is an "example", and rather then generate some fake Nim code, I demonstrated the issue simpl

How to escape colon in the '&' macro in strformat ?

2021-05-22 Thread stbalbach
This works: import strformat echo &"""{"url = http//example.com"}""" Run This does not: import strformat echo &"""{"url = http://example.com"}"""; Run Colon is the problem.

Regex and capture unicode text

2021-01-21 Thread stbalbach
That's great, thank you, did not know this. Now understand the issue and the possible solution.

Regex and capture unicode text

2021-01-21 Thread stbalbach
Working with various languages in Wikipedia and would like to capture text that is Unicode, for example: This works (plain ascii): import re let t = "{{Cite book|test=}}" echo $(findBounds(t, re("(*UTF8)[{]{2}Cite book[|][^}]+}}", {}) )) Run This does not work

encodeRe()

2021-01-07 Thread stbalbach
Program: import re echo escapeRe("[Одиноков]") echo escapeRe("[Oklahoma]") Run Produces: \x5B\xD0\x9E\xD0\xB4\xD0\xB8\xD0\xBD\xD0\xBE\xD0\xBA\xD0\xBE\xD0\xB2\x5D \x5BOklahoma\x5D Run The Oklahoma is right, but not the Greek word. R

Creating unique ID from strings

2020-11-14 Thread stbalbach
128 bit.. then murmur3 might be better, nearly as fast as xxhash and about 10x faster than SHA

Creating unique ID from strings

2020-11-14 Thread stbalbach
Excellent, thank you for the leads. Based on information from / I'll try xxhash from .. it appears to be very fast and has many cross language.platform versions that are consistent in result. I am new the world of

Creating unique ID from strings

2020-11-13 Thread stbalbach
Given millions of records -- specifically citation templates on Wikipedia. Example citation: {{Akademik dergi kaynağı|başlık=Air Pacific Ltd. History|tarih=2005|çalışma=International Directory|yayıncı=St. James Press|cilt=70}} Run These are to be stored in a key-valu

How Can I Convert An Integer to String?

2020-09-19 Thread stbalbach
To go the other way (string to int) is a proc called parseInt(). To make it a bit more robust: # # str2int([string], [default result]) # # . [default result] is an optional return value if parseInt() has an exception #If not default result and exception then re

global var not detected by compiles()

2020-08-31 Thread stbalbach
Ah thank you missed the elephant for the ghost

global var not detected by compiles()

2020-08-31 Thread stbalbach
Given this: template test(k: untyped) = when compiles(k): echo "it compiled" k = "New England" else: echo "not compiled" var k = "New England" proc main() = test(pagekey) var pagekey = "England"