python

This magazine is from a federated server and may be incomplete. Browse more on the original instance.

e0qdk, in Problems parsing a string with pyparsing
e0qdk avatar

Haven't used that particular library, but have written libraries that do similar sorts of things and have played with a few other similar libraries in C++ and Haskell. I've taken a quick glance at the documentation here, but since I don't know this library specifically apologizes in advance if I make a mistake.

For OneOrMore(Word(alphanums)) + OneOrMore(Char(printables)) it looks it matches as many alphanum Words as it can (whitespace sequences being an acceptable separator between tokens by default) and when it hits ( it cannot continue with that so tries to match the next expression in the sequence. (i.e. OneOrMore(Char(printables)))

The documentation says:

Char - a convenience form of Word that will match just a single character from a string of matching characters

Presumably, that means it will not group the characters together, which is why you get individual character matches after that point for all the remaining non-whitespace characters. (Your result also seems to imply there was a semicolon at the end of your input?)

For OneOrMore(Word(alphanums)) + OneOrMore(Char(string.punctuation)) it looks like it cannot match further than ( since 1 is not a punctuation character; so, you got the tokens for the parts of the string that matched. (If you chained the parser expression with something like + Word(alphanum) I'd expect you'd get another token [i.e. "1"] added onto the end of your result.) You may eventually want StringEnd/LineEnd or something like that -- I'd expect they'd fail the parser expression if there's unconsumed input (for error detection), but again, haven't used this specific library, so it may work different than I expect.

There appears to be a Combine class you can use to join string results together; that might be useful for future reference.

i was trying to parse a string with pyparsing so all the words were separated from the punctuation signs

Have not tested it (since I don't have a copy of the library installed anywhere and can't set up an environment for it easily right now) but perhaps something like OneOrMore(Word(alphanums)|Char(string.punctuation)) would be more like what you are looking for?

beautiful_boater, in Python developers won’t let go of Python 2

Well, TBF there is a lot of avenues to get locked into legacy software in python. I am still modifying and using Python2 code because the drivers and libraries for hardware are only available in python2 and the hardware developers wont spend the money and time to create Python3 libraries. So I am stuck using an airbridged, un-updated python2 environment until it gets to the point of updating/backwards engineering python3 drivers and libraries for all our hardware ourselves.

naonintendois,

What hardware drivers are written in Python? I would assume they’re bindings to drivers with a C interface.

hosaka,

Take a look at micropython, some drivers are writing in pure python, I’ve written a display driver previously

naonintendois,

I assumed those were for hobbyists. I would be surprised if that was used in a major product given how many cycles you lose to python implementation.

hosaka,

Yeah you’re totally right. Nonetheless the use case has it’s place. People buy and use hobbyist hardware, and this is a market on its own.

beautiful_boater,

I should have been more specific. Yes, it is a wrapper around a closed source blob.

Theharpyeagle, (edited ) in Reading the Python Official Documentation is rugged

I highly recommend visiting the Python discord (discord.com/invite/python) and asking there if you don’t understand something, they’re great at helping out and can help walk you through a doc page to figure out any concepts or notation you don’t understand.

Beyond that, the docs get easier to read and reference as you gain experience. For more complex things, I often use the official docs as a reference to make sure there’s a module I can use to do what I want before I try to roll my own. Then I tend to look up examples elsewhere (often realpython and SO). Do you have an example of something you’re struggling with?

Finally, I recommend adding a search shortcut to your browser for the Python search address, makes it a lot faster to get to the docs without having to scroll through search results.

elouboub, in Microsoft is bringing Python to Excel
elouboub avatar

Does LibreOffice support python? because VB is cancer

TheCee,

Yes.

LollerCorleone,
LollerCorleone avatar

Already does.

ImpossibleRubiksCube,

Technically LibreOffice uses UNO, their own environment; but it’s really easy to work with Python from it. So, I would say yes.

help.libreoffice.org/…/python_programming.html

Vector, in RPi 0W - How to stop long-running action

It should be possible, but the answer is going to depend on your implementation, what libraries you are using, and so on.

For example, if the play sound action is synchronous, then maybe you could start it up in another thread, and interrupt that thread if you want to cancel the sound.

If it’s asynchronous, maybe you need to retain a reference to the sound object and then invoke a stop() call when the other button is pressed.

logging_strict, in LPython 0.21 Released For Alpha-Stage Python AOT Compiler

CPU

pep744

GPU – NVidia

numba

logging_strict, in LPython 0.21 Released For Alpha-Stage Python AOT Compiler

LPython will be obsoleted by Mojo

mapto, in Autorun | Autostart | Run at startup
@mapto@lemmy.world avatar

Have you looked at this one? pypi.org/project/onboot/

conductor, (edited )

Thank you, I think that is what I need)

logging_strict, (edited ) in A Guide to Python Lambda Functions

BFF 1: Chuck is going to be so surprised. We’ve hired the best caterers, cosplay theme, fireworks, yard ales, twister themed blind laser tag, there will be balloons, ball pool, athletic challenge course, fabulous entertainment from top notch talent, show girls, the wine will flow like water

BFF 2: Tell me you didn’t invite that grifter, mypy

BFF 1: I … err … hum. Opps

mypy enters the conversation

Don’t use lambda use def function instead

BFF 2: Man i hate that guy. Rains on our parade and sucks the fun out of the room (and this entire thread).

lascapi, in A Guide to Python Lambda Functions
@lascapi@jlai.lu avatar

I discover the operator module! Amazing! 🤩

The operator Module A third alternative to writing lambda functions is to use the standard library’s operator module. This module contains some predefined functions and function factories, which can replace the most common use cases for lambda functions. Let’s look at both of these separtaely, factories first. Another note: the function that attrgetter returns is implemented in C, so it’s slightly faster than using either a normal or lambda function.

Useful if you want to speed up your code.

driving_crooner, in A Guide to Python Lambda Functions
@driving_crooner@lemmy.eco.br avatar

I use lambda functions on my panda’s data frames when I need to do row operations, with .apply().

tunetardis, in A Guide to Python Lambda Functions

They’re somewhat more capable now that we have the walrus (:=) operator.

BeefPiano,

Can you give an example? Can you use it to initialize vars outside the scope of the lambda?

sugar_in_your_tea,

The general form is:


<span style="color:#323232;">(var := expression) rest of condition
</span>

So you can do something awful like this:


<span style="color:#323232;">lambda: (y := 1) != 2 and y
</span>

Not sure if that’s “good,” but it does kind of let you sneak a statement into the lambda.

tunetardis,

Can you use it to initialize vars outside the scope of the lambda?

No, that’s not what it’s for. It lets you define a temporary local variable within an expression. This is useful in situations where you might want to use the same value more than once within the expression. In a regular function, you would just define a variable first and then use it as many times as you want. But until the walrus operator came along, you couldn’t define a variable within a lambda expression.

Can you give an example?

Ok, I’m trying to think of a simple example. Let’s say you had a database that maps student IDs to records contain their names. To keep things simple, I’ll just make it plain old dict. And then you have a list of student IDs. You want to sort these IDs using the student names in the form “last, first” as the key. So you could go:


<span style="color:#323232;">>>> student_recs = {1261456: {"first": "Harry", "last": "Potter"}, 532153: {"first": "Ron", "last": "Weasley"}, 632453: {"first": "Hermione", "last": "Granger"}}
</span><span style="color:#323232;">>>> student_ids = [1261456, 532153, 632453]
</span><span style="color:#323232;">>>> sorted(student_ids, key = lambda i: (rec := student_recs[i])['last'] + ', ' +  rec['first'])
</span><span style="color:#323232;">[632453, 1261456, 532153]
</span>

The problem here is that student_ids doesn’t contain the student names. You need use the ID to look up the record that contains those. So let’s say the first ID i is 1261456. That would mean:


<span style="color:#323232;">rec := student_recs[i]
</span>

evaluates to:


<span style="color:#323232;">{"first": "Harry", "last": "Potter"}
</span>

Then we are effectively going:


<span style="color:#323232;"> rec['last'] + ', ' + rec['first']
</span>

which should give us:


<span style="color:#323232;"> 'Potter, Harry'
</span>

Without the := you would either have to perform 2 student_recs[i] look-ups to get each name which would be wasteful or replace the lambda with a regular function where you can write rec = student_recs[i] on its own line and then use it.

Am I making any sense?

tunetardis,

Actually, now that I think of it, there’s no reason you need to join the 2 names into a single str. You could just leave it as a tuple of last, first and Python will know what to do in comparing them.


<span style="color:#323232;">>>> sorted(student_ids, key = lambda i: ((rec := student_recs[i])['last'], rec['first']))
</span><span style="color:#323232;">[632453, 1261456, 532153]
</span>

So the lambda would be returning (‘Potter’, ‘Harry’) rather than ‘Potter, Harry’. But whatever. The := part is still the same.

celliern, in A library for creating fully typed declarative API clients quickly and easily

Nice! Was looking for an Uplink alternative that is properly typed, will try that for my next project

martinn,

Glad to hear that! Let me know how it goes.

CaptPretentious, in A library for creating fully typed declarative API clients quickly and easily

Commenting so I remember to check this out tomorrow

SatouKazuma,
@SatouKazuma@lemmy.world avatar

Ditto. The state management will be a boon for me at work.

martinn,

Nice! Keen to hear how you go. Any issues at all just let me know.

stanka, in A library for creating fully typed declarative API clients quickly and easily

Looks cool. I’ve never really considered an engine like this, what else exists like this right now?

martinn,

Thanks! There’s a few “similar” projects that I’ve seen but IMHO not as clean or easy to use.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python@programming.dev
  • DreamBathrooms
  • mdbf
  • ngwrru68w68
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • osvaldo12
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • InstantRegret
  • GTA5RPClips
  • provamag3
  • ethstaker
  • cisconetworking
  • tester
  • modclub
  • everett
  • cubers
  • tacticalgear
  • Leos
  • megavids
  • normalnudes
  • anitta
  • JUstTest
  • lostlight
  • All magazines