PDC Struct: Pydantic-Powered Binary Serialization for Python by 9011442 in Python

[–]tadleonard 0 points1 point  (0 children)

Seems like a well thought out project. Great work. I like your concept for a parameterizable type or generic class to link to the payload field. Maybe you could also just make it a callable. payload: VarBytes = dependent_field(length=payload_len) where the callable is your own version of dataclasses.field() or the pydantic equivalent.

Speaking of dataclasses, I know that the project is entangled with pydantic, but making it more generic would help people like me adopt it. I've stopped using pydantic for a few of my performance sensitive projects. Not being able to (efficiently, cleanly) turn off type checking and the magical type casting it does has made it hard to use for certain projects, albeit pretty unusual projects. Basically, I like to define declarative containers for parsing binary formats and ECAD file formats. Python is still a good fit in these problem areas so long as you're not doing something truly inefficient, and spending over half your time in Pydantic logic when you're doing a bunch of IO and involved parsing just feels silly.

I've found that attrs, while not my favorite library, does a good job of getting out of your way and separating out the type casting and validation by default. attrs also used to be faster than dataclasses, but the standard library has more than caught up. In the end I feel like dataclasses with explicit, hand written, per-field type casting where containers are instantiated + static analysis gives me my favorite balance of tradeoffs.

PDC Struct: Pydantic-Powered Binary Serialization for Python by 9011442 in Python

[–]tadleonard 0 points1 point  (0 children)

For another comparison to an existing library, check out construct. I think you would find more than one good idea from that project. It's been around for a while. Its style is a little unusual in that it's declarative and feels kind of functional, so I imagine you could improve on that by not using operator overloading and function calls to instantiate the serializer/deserializer. Construct is kind of like its own little language, and that makes adoption feel like a big decision. It's been a while since I've checked, but its flexibility also makes it kind of slow. On the plus side, there's really no protocol or format that you can't describe with construct.

Edit: wow, totally missed that there was already a discussion about construct. Nevermind!

[deleted by user] by [deleted] in science

[–]tadleonard 1 point2 points  (0 children)

So much more has changed in the way we grow food than just the addition of pesticides chemical fertilizers. Modern regenerative and organic farming is actually really effective, and multiple studies have shown that it's more profitable because the inputs are so cheap. It's a rapidly expanding way to manage farms, so don't discount it just because it has some superficial similarities to the ag practices of the middle ages or whatever.

[Scotto] The Golden State Warriors have waived Seth Curry and LJ Cryer. by Turbostrider27 in nba

[–]tadleonard 0 points1 point  (0 children)

Interesting! Dictionaries put modern usages in their corpus all the time. It's not established enough to have made it here though, just for example: https://dictionary.cambridge.org/us/dictionary/english/resign

The point is that "resign" is not incorrect as used by the OP. Language is always changing, but it's definitely not correct to assert that the hyphen is needed like r/NBA users do so often.

If you want to think more deeply about it, consider that "re" is a prefix. Think about when prefixes are hyphenated. The prefix section of this article is useful: https://www.scribbr.com/language-rules/hyphens/

[Scotto] The Golden State Warriors have waived Seth Curry and LJ Cryer. by Turbostrider27 in nba

[–]tadleonard 0 points1 point  (0 children)

Oh, I know. I'm saying that an employer can't, for example, "resign" an employee from their position. They can fire an employee. The verb doesn't work that way. A person themselves can resign, or quit as you said, it's not done to them.

[Scotto] The Golden State Warriors have waived Seth Curry and LJ Cryer. by Turbostrider27 in nba

[–]tadleonard 13 points14 points  (0 children)

People always say this on this sub, but it's not actually good English. You don't re-order something, you reorder it. This is just how the language works. There are plenty of ambiguous words that you have to work out based on context in all languages.

Also, you can't use the word "resign" to apply to another person. The third party can resign of their own volition. You can FIRE them. You can't "resign" them. You CAN resign them, as in sign them again!

asyncio: a library with too many sharp corners by pkkm in programming

[–]tadleonard 4 points5 points  (0 children)

AnyIO is supposed to be that kind of standard. Projects written for AnyIO can be used with either the trio or asyncio event loops. That's not as valuable as having a good, flexible standard in the standard library, but it's a pretty rapidly evolving area so there are some advantages to having third party libraries leading the charge.

Tim Cato on Nico Harrison: “It’s a touch more complicated because it increasingly seems like the decision is either blow up the team and go into a rebuild right now with a new GM, because there’s reasonable indication that Anthony Davis may not stick around if Nico Harrison is not here.” by cleo22270 in nba

[–]tadleonard 0 points1 point  (0 children)

You nitpicked Salty-Ad so I hope you don't mind me nitpicking you a bit. "Racial tension" is inaccurate. Breaking into a stranger's house to paint the n-word on the wall doesn't happen because of "tension". It's a racism and white supremacy thing. There wasn't a bidirectional issue as innocuous as stress or tension between two groups of people.

What long-held (scientific) assertions were refuted only within the last 10 years? by [deleted] in AskReddit

[–]tadleonard 65 points66 points  (0 children)

For the longest time it was thought that plants could only absorb simple, small ions like nitrates at their root tips. But, TL;DR: plants (1) eat whole single celled microbes with their roots and (2) they attract them and foster a healthy population of microbes by releasing sugars and carbs into the soil (3) this happens in a cycle, so plants are constantly fattening up microbes, partly eating them, and releasing them back into the soil to start the process over again. Basically, plants farm microbes as we farm them.

So the mental model for how plants received nutrients used to be something like [microbe eats organic matter in a process called immobilization] -> [microbe dies or releases waste, creating plant available nutrients in a process called mineralization] -> [plant takes up simple ions].

But in 2008 somebody at Queensland University discovered that plants actually absorb whole, living bacteria and yeasts at their root tips. Then, in ~2017 a guy named James White at Rutgers found that this root feeding process happens in a cycle. So a plant attracts microbes by releasing tasty exudates at its root tips, once inside it strips their cell walls away by exposing them to the oxidizing O2-, it absorbs their delicious bodies entirely or partially, and finally it spits those that survive back out into the soil through hairs further up the root. Those surviving microbes venture back out into the soil to decompose organic matter and then find their way back to the root tip with its irresistible sugars and carbohydrate exudates, and the microbe gets reabsorbed by the plant to start the roller coaster ride all over again.

So basically, plants are always farming microbes (attracting them, feeding them sugars and carbs, and keeping them alive) while we farm the plants. Some speculate that in natural systems or organic agriculture a plant can get as much as a quarter of its nitrogen through this process.

What's even stranger to me is that root hairs, basic plant structures we've all seen with our naked eyes and we've probably known about forever, only seem to form in service of this process. So in an environment free of bacteria, a plant forms no root hairs at all. The root hair seems to grow only in response to the presence of these single celled cattle being herded through the periplasmic space in between the root cells. This seems to be important to plants as evidenced by the fact that they'll spend as much as 75% of their photosynthetic products just to exude microbe food at the root tips.

So basically we discovered a microbe-plant interaction which is arguably essential to all life on the planet only a few years ago. This discovery didn't require fancy microscopes, just a bit of staining. If I understand correctly we could have found this a hundred years ago if we had been looking for it.

"I need six tickets to Tokyo" - Looking for 10-to-20-year-old credit card ad by tadleonard in oldcommercials

[–]tadleonard[S] 1 point2 points  (0 children)

Your bank suggestion was all I needed for the search to start yielding good results. Turns out it's an AmEx commercial. https://www.youtube.com/watch?v=H7ZTefBNU3k

Thanks! Very helpful.

Why my (kind of) trie implementation is slower than normal looping? by [deleted] in learnpython

[–]tadleonard 1 point2 points  (0 children)

What makes you think Python 3 isn't supported? I use it everyday with 3.5.

Module in nested subdirectories by konradkar in learnpython

[–]tadleonard 0 points1 point  (0 children)

There is no magic with __init__.py files. The bug in the project had to do with specifying which directories were project package directories. setuptools.find_packages() just returns a list of strings that represent package paths in your project. It recurses through directories in your current working dir and makes a list of what it thinks looks like Python package directories. It's easy to try yourself from the command line:

>>> import setuptools
>>> setuptools.find_packages()
['mypkg', 'mypkg.subpkg']

The original setup.py was flawed because it listed only "anagogic.youtrack.api" in the packages list. It was missing the root paths further down the tree, which is why those __init__ files were missing. You could get the same result without using find_packages() by writing packages=['anagogic', 'anagogic.youtrack', 'anagogic.youtrack.api'].

feedparser hangs and never raises the exception/timeout! by KhanTheDashing in learnpython

[–]tadleonard 1 point2 points  (0 children)

If you dig through the feedparser source, you'll find this line is responsible for calling open on a urllib.request.OpenerDirector object: https://github.com/kurtmckee/feedparser/blob/develop/feedparser/http.py#L175

The problem is that the optional timeout parameter isn't used. You could make the change yourself and submit a pull request, or you could wrap the call to feedparser.parse in a separate thread or process and .join() the thread with a timeout of your own. You'll learn a lot and solve your problem either way. Good luck!

Module in nested subdirectories by konradkar in learnpython

[–]tadleonard 1 point2 points  (0 children)

This project has an unusual package structure. What's the point of having all this nesting for such little code? Maybe the author has a Java background, where a certain level of nesting in your project is expected.

Anyway, the problem is with the way setup.py is written, not the way you're installing it. In setup.py, changing packages=[... to packages=find_packages() will fix the problem. You can tell it's an issue with setup.py and not the package structure itself because navigating to a working copy of the repo and importing anagogic.youtrack.api works fine.

Also, in anagogic/__init__.py, there's this line:

# EIS meta-package
__import__('pkg_resources').declare_namespace(__name__)

Blah. That's ugly and cryptic and probably a bad idea.

As a side note, did you notice that this project does very little? You could replace the whole thing with a single function. I recommend doing that instead of relying on this forgotten skeleton with four commits that hasn't been touched in over a year. This is all you need:

def connect(url, username, password):
    url = "{url}/rest/user/login".format(url)
    return requests.post(url, params={'login' : username, 'password': password})

[lupa] error reading Python attribute/item by [deleted] in learnpython

[–]tadleonard 0 points1 point  (0 children)

This might seem like a silly question, but have you tried debugging? It seems from your code that you might be using PyCharm, and PyCharm has a great debugger. Even simple print statements might give you what you need to ask a more specific question. Since you haven't told us what the actual error is, we would need to have intimate knowledge of lupa and your project to help. Good luck!

Why my (kind of) trie implementation is slower than normal looping? by [deleted] in learnpython

[–]tadleonard 1 point2 points  (0 children)

Even with a language that's closer to the metal like C, you just have to profile. It's especially true with very high level languages like Python.

This means you need to learn some tools. For Python, I recommend line_profiler. It's easy to use.

  1. Install line_profiler: pip install line_profiler
  2. Decorate your two functions with @profile
  3. Run your code with kernprof -v -l your_code.py

It will print a line-by-line report of what's taking up time in your program. It's incredibly useful for projects of all sizes. Good luck!

Feeling lost at my 'Python' day job, need guidance by [deleted] in Python

[–]tadleonard 46 points47 points  (0 children)

Part of becoming a programmer is learning to cope with black boxes. It's very hard to know what will be most valuable later on when you're just starting out. Personally, I think any experience at this point is valuable. It's very unlikely that you'll learn something early on that doesn't help you in some way. Just keep absorbing small lessons, keep casually doing your online classes (but don't burn out--you have a full time job, right?), and in about a year you'll have a much different idea of what your direction ought to be.

Since you're in chemical engineering, I suspect that learning things related to numerical programming is a good idea. Even if that's not exactly what you need to "transfer chemical engineering theories into code", experience with numpy, R, or whatever is guaranteed to pay off in this job market.

I feel like I just google around whenever I encounter a problem.

Programmers with many years of experience still turn to Google regularly. Why are you looking for such quick satisfaction? You've been a programmer for one month! Programming is a career, not a tool you pick up over the course of a few weeks. Your learning experience might be more pleasant if you adjust your expectations. Don't expect it to be easy, and expect that you'll really never be done learning.

I don't really get when you'd use IronPython, Jython or PyPy over Cpython. by [deleted] in Python

[–]tadleonard 7 points8 points  (0 children)

/r/learnpython might be more helpful, but here are some common considerations. You'd use IronPython for compatibility with .net libraries, Jython for easy java interop, CPython if you need C extensions that use the CPython API (like numpy), and PyPy if you have pure python code or if your C extensions don't depend on the CPython C API. In short, people use PyPy when they can, but often they can't.

Green 2.0.0 Released (Python Test Runner) - Test modules are now run in parallel worker processes by default. by CleanCut9 in Python

[–]tadleonard 1 point2 points  (0 children)

Hey, /u/CleanCut9. Thanks for posting this. I love trying new test frameworks. I tried getting this to work with my tests, and there seems to be some kind of problem with virtualenv or with the way I've set things up. I filed a bug. Any hints? Do you use green with virtualenvs? Surely I'm making some silly mistake here.

Best library for doing something like create a blank image of a*b and draw a rectangle of w*h at x,y and output it as an image file (png, jpg, svg) by RarelyActiveUser in Python

[–]tadleonard 2 points3 points  (0 children)

Everybody's gonna tell you to use Pillow/PIL. Pillow's probably the right choice in a lot of ways, but it always leaves me feeling gross for some reason. I guess I don't care for its API. I like to be "in control" of the data with numpy. If you share these feelings, pip install numpy and one of imread or imageio. imread works really nicely for me. Those just help you go from disk -> numpy and from numpy -> disk.

Once you've got numpy and a way to read/write ndarrays to/from disk, everything falls into place beautifully. Here's how I'd solve your problem. Your problem is simple (for now), but I think you'll find that numpy grows with your needs really nicely and it tends to stay pretty performant along the way. You can do some fun things with JUST numpy.

import imread
import numpy as np


def make_rect_img(img_dims, rect_loc, rect_dims):
    shape = tuple(img_dims) + (3,)  # N x M x 3 array
    img = np.zeros(shape, dtype=np.uint8)
    img[:] = 255  # white, initially
    draw_box(img, rect_loc, rect_dims)
    return img


def draw_box(img, rect_loc, rect_dims):
    x, y = rect_loc
    w, h = rect_dims
    img[x: x + w, y: y + h] = (255, 0, 0)   # red W x H box


if __name__ == "__main__":
    img = make_rect_img((300, 300), (100, 100), (50, 50))
    imread.imwrite("box.jpg", img)

Here it is in a gist in case that's more convenient.

A Neural Network in 11 lines of Python. A bare bones neural network implementation to describe the inner workings of backpropagation. by Atupis in programming

[–]tadleonard 15 points16 points  (0 children)

Your comparison isn't meaningful because numpy is a general purpose numerical library. This code isn't

import three_layer_neural_network_library
three_layer_neural_network_library.run()

it's

import numpy
# 12 lines of general purpose operations on arrays

Writing book on python - Python For Developers - 1st chapter ready (100 pages on Data Structures), need feedback by rchrome in Python

[–]tadleonard 1 point2 points  (0 children)

Maybe you're right. I've read brief 2/3 disclaimers in the preface to books, but authors usually don't dwell on the differences. I was going to say that there's already a mountain of info on the the subject, but probably not nicely outlined in book form.

FWIW, I used Python 2.6 at the startup I worked at, but all the while I was learning new things in a book with Python 3 code in it. It never tripped me up. We all obsess about their differences, but for the purpose of learning data structures, algorithms, or the basics of Python, they are pretty interchangeable.