question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document how to use a `--convert` function that runs initialization code first

See original GitHub issue

When I have an insert command with transform like this:

cat items.json | jq '.data' | sqlite-utils insert listings.db listings - --convert '
d = enchant.Dict("en_US")
row["is_dictionary_word"] = d.check(row["name"])
'  --import=enchant --ignore

I noticed as the number of rows increases the operation becomes quite slow, likely due to the creation of the d = enchant.Dict("en_US") object for each row. Is there a way to share that instance d between transform function calls, like a shared context?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
simonwcommented, Mar 28, 2022

So now this should solve your problem:

echo '[{"name": "notaword"}, {"name": "word"}]
' | python3 -m sqlite_utils insert listings.db listings - --convert '
import enchant
d = enchant.Dict("en_US")

def convert(row):
    global d
    row["is_dictionary_word"] = d.check(row["name"])
'
0reactions
simonwcommented, Aug 28, 2022

I found a fix that makes that global workaround unnecessary:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code First Migrations - EF6 - Microsoft Learn
Generating & Running Migrations · Run the Update-Database command in Package Manager Console · Code First Migrations will compare the migrations ...
Read more >
Object initializer - JavaScript - MDN Web Docs - Mozilla
An object initializer is a comma-delimited list of zero or more pairs of property names and associated values of an object, enclosed in...
Read more >
Understanding init in Go | DigitalOcean
Introduction. In Go, the predefined init() function sets off a piece of code to run before any other part of your package.
Read more >
Initialization — The Swift Programming Language (Swift 5.7)
Both initializers convert their single argument into the corresponding Celsius value and store this value in a property called temperatureInCelsius . Parameter ...
Read more >
The Go init Function | TutorialEdge.net
In this tutorial we'll be looking at the Go init function, how to use it and some of the things to consider when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found