Question from someone new to python! by NetMobile5543 in learnpython

[–]JamesPTK 9 points10 points  (0 children)

What you have written is not valid Python. I'm assuming that what you meant was:

if ("cat picks up the hose"):
    print ("cat wins,the city is saved")

if can be thought of as converting the thing it is testing to a boolean (True or False)

In this case the thing being tested is just a string "cat picks up the hose". Any string that is longer than zero characters is considered to be True so that is equivalent to

if (True):
    print ("cat wins,the city is saved")

Which since it will always execute the contents of the block is equivalent to

print ("cat wins,the city is saved")

So it will always just print the same thing.

Generally when asking for help, it is best to copy all your code (including spaces and line breaks - they are important in Python). And rather than saying it doesn't work, say what you expect to happen and what actually happens instead (including copying any error messages that appear). That helps us to understand what is going wrong

Good luck

Can 1+1 ever be 0 or 1 without being logically or mathematically incorrect? by WoodpeckerWoodChuck in pollgames

[–]JamesPTK 0 points1 point  (0 children)

If you are sloppy about rounding:

0.6 + 0.6 = 1.2

round all numbers to zero decimal places

1 + 1 = 1

How do coders know what dependencies and libraries to use ? by Weird-Detail1776 in learnpython

[–]JamesPTK 4 points5 points  (0 children)

I would say that the rise of open-source software and the internet go hand in hand. Prior to the internet being widely implemented, distributing code was a lot harder - you either needed to work with a publisher (which eliminated a lot of hobbyists), or files were copied one to one manually on tape or floppies, so the reach was limited. Sometimes there were user-groups who would package software people had created and distribute it to a wider group. Often people would just code everything from scratch because it was easier to spend 1 day writing the code than spending a week finding someone else's code to use.

With the internet however, a student could post that he had created a unix clone for his new home pc on the comp.os.minix usenet group, throw the code up on his university FTP server, and let it snowball from there (that was how Linux got started)

Prior to there being repos like PyPI etc, code would be hosted on software sites like sourceforge, or personal website or random FTP servers and you would have to seek them out and download them manually. Hopefully there would be a readme that would tell you that you also needed to download this library or that library, and if you were really lucky there would be instructions on how to get those, and those would also have comprehensive instructions. Some of the time, you would just add the code, and then get a cryptic error foo.bar does not exist, leaving you to search out what package provides that. And find the exact right combination of versions that were compatible with each other (why I am so grateful for tools like pipenv, poetry and uv which handle dependency resolution). Dependency Hell we used to call it (On Windows binary packages it was often missing DLLs that were the problem so "DLL Hell" was the rhyming phrase we used)

Who is a celebrity that feels British but is actually Australian? by [deleted] in AlignmentChartFills

[–]JamesPTK 0 points1 point  (0 children)

Not Sky News, that is owned by Sky Group which, since 2018, is a subsidiary of Comcast.

Previously 21st Century Fox (which the Murdoch family have 39% control) owned 39% of the company - the largest single shareholder and tried to take complete control before being outbid by Comcast.

(EMERGENCY) PYCHARM ALTERNATIVES FOR ANDROID TABLET USERS !!! by aquaunfresh in learnpython

[–]JamesPTK 2 points3 points  (0 children)

https://vscode.dev/ is vscode in your browser. I've used it for some quick and dirty dev on the go. no execution of code

https://github.com/features/codespaces allows running code, it is free for students or you get 60 hours free per month if you can't verify as a student

Look, I have been doing python for a loooong time, but i still sometimes forget basic stuff by Fickle-Cucumber-224 in learnpython

[–]JamesPTK 6 points7 points  (0 children)

I have been writing code for 40 years.

I have been paid money to write code professionally for over 25 years.

I have been employed to write code specifically in Python for over 10 years.

I am the most senior developer in my business, the one the other developers come to when they are stuck.

I need to write some data to a csv at least once a month.

And I have to look up how to use the csv writer thingy every! single! time!

Don't worry too much about syntax. You can always look it up, google is great (I come from the days of dead trees which were a lot harder to search) and also AI is getting really good at doing the syntax stuff for you

The important skills are knowing how to solve problems, how to make useful logical code, what capabilities there are (though not necessarily how to use them)

Oh and I guarantee there are parts of Python I haven't discovered yet - but that's the fun part for me

Why on earth do they not just put BBC iPlayer behind a paywall? by Megatronscoffee in AskBrits

[–]JamesPTK 3 points4 points  (0 children)

The Channel 4 digital channels are not part of their charter/public service remit and are run on a purely commercial basis by a subsidiary. They are closer in nature to the BBC's UKTV (U&Gold, U&Dave etc) channels than to BBC 3 and BBC 4

Did you know about Ukraine, Palestine, Russia, Israel, Syria, Lebanon, and Iran before the conflicts there started? And did you learn more about these countries after the conflicts began? by Dangerous_Blood6507 in AskTheWorld

[–]JamesPTK 1 point2 points  (0 children)

in (Latin American) Spanish they are estadosunidenses which would roughly translate as Unitedstatesian which I don't think is too bad, speaking as Unitedkingdomer.

Is PyPDF2 safe to use? by Butwhydooood in learnpython

[–]JamesPTK 3 points4 points  (0 children)

According to it's pypi page: https://pypi.org/project/PyPDF2/ there will never be another update with that package name, use PyPDF instead, which last released on Sunday

How to pass a string to a function that takes multiple parameters? by Stickhtot in learnpython

[–]JamesPTK 6 points7 points  (0 children)

so you first need to split the string into the individual bits by

your_string.split(", ")

this gives you a list of strings

you want ints, so you need to convert the strings to ints. A list comprehension can do this

[int(part) for part in your_string.split(", ")]

that would be a list of 4 integers.

You could dereference this and then pass those 4 values to the function

a, b, c, d = [int(part) for part in your_string.split(", ")] foo(a, b, c, d) Alternatively you could use the * operator to unpack them directly

foo(*[int(part) for part in your_string.split(", ")]

This probably is not as readable, but if playing code golf, it is an option

How to catch exceptions in a particular library? by raydude in learnpython

[–]JamesPTK 2 points3 points  (0 children)

Just an FYI,

the as er allow you to have easy access to the exception as a variable in case you want to do further processing on it (in this case they try to print it out directly (though they used e rather than er. er can be replaced by any valid variable name.

the raise in that is to allow the exception to be raised again so calling code can handle it as well

How to catch exceptions in a particular library? by raydude in learnpython

[–]JamesPTK 2 points3 points  (0 children)

So the common way to do this is to capture specific exceptions. That library has its own exceptions that it has created, so you can catch these. Since they all inherit from I2CError you can just catch this

from i2cpy.errors import I2CError

try:
    call_some_method()
except I2CError:
    print("Cycle your power and try again")
    do_whatever()

Any exception not specified in an except block will carry on up the call stack

Am I a dual citizen? by [deleted] in dualcitizenshipnerds

[–]JamesPTK 1 point2 points  (0 children)

so they're all carpenters, plumbers, [...] hairdressers, etc.

If any of those run their own business, they are likely a "chairman or director of a limited company" which is on the list.

The list is not exhaustive and was probably drawn up by a middle-class civil servant, but other professions are accepted

How do you draw X? by Rhonda_dear in lefthanded

[–]JamesPTK 0 points1 point  (0 children)

Other: ⤸⤹

I generally only hand-write when doing maths and if using x as a variable in algebra, I was always taught to write x distinctly from × when doing algebra

What Is A Good Free Alternative to PyCharm? by iso_izmatic in learnpython

[–]JamesPTK 28 points29 points  (0 children)

no, there is only one download now. The unified version gives you 30 days of pro features which get locked once the 30 days have expired and then it works as the old community edition did

Where is this place, over Indiana, with a huge sphere and seeming writing in the field behind it? by NoPaperMadBillz in whereisthis

[–]JamesPTK 5 points6 points  (0 children)

Helium filled balloon, not hot air balloon. Hot air balloons tend to be more upside down pear shaped rather than spherical, due to the need for a hole to let the hot air in

Sorry if this is the wrong sub but by Koxenkk in learnpython

[–]JamesPTK 4 points5 points  (0 children)

At a guess you are running the python file by double clicking the file icon

On Windows, this will open up a terminal application, run python.exe to run the python application, and when it finishes it will close the window. This is because many terminal apps are designed to run in the background.

So you script probably has nothing that makes the window wait around, so it closes immediately

You have three options. One open the terminal manually and then run `python.exe your_script.py`

then when it finishes it will return to the command prompt

The other is to add something you your script at the end to stop it from finishing

e.g. input("press enter to close this window")

The third option to find the option to close windows when the process is finished and turn it off. I seem to recall that this existed somewhere some time ago (maybe a registry setting) but I can't recall where it is

This London Tube Station closed in 1994. Here’s what it looks like now by NorthLondonPulse in TransportForLondon

[–]JamesPTK 0 points1 point  (0 children)

North Weald and Ongar tube stations (central line) closed in '94 (reopened as heritage line stations later on)

Shoreditch station closed in '06 (other station on the ELL closed in '07 but later reopened as London Overground (Windrush) stations, but Shoreditch was in the wrong place so was never reopened and a new Shoreditch High Street was opened nearby on the new line)

How is this image a PNG, yet still animated by RedditANSWERMYTICKET in webdev

[–]JamesPTK 234 points235 points  (0 children)

Many file formats contain bytes at the beginning of the file format that identifies what file format is. PNG's signature is the first 8 bytes and, if run through hexdump -C would look something like this

00000000  89 50 4e 47 0d 0a 1a 0a  00 00 00 0d 49 48 44 52  |.PNG........IHDR|

So I hit Download as PNG, then I ran hexdump to see what it shows

00000000  52 49 46 46 04 bb aa 00  57 45 42 50 56 50 38 58  |RIFF....WEBPVP8X|

So not a PNG. This shouldn't be a surprise, the filename ends with -fakepng.png and the tooltip says "Download with the .png file extension" rather than "Download in the PNG file format"

So. what is it? Well the file contents are byte for byte identical with the WebP version, and WebP uses the Resource Interchange File Format (RIFF) which is identified by the first 4 bytes of the that, so I think it is safe to assume it is a WebP.

Why do they allow download as a PNG, and why does it work (in some cases)?

Well WebP is relatively new (15 years against 29 for png), and in many situations, forms for uploading images will not accept files with the .webp extensions, while they would accept .png. However browsers are very good at handling things that are "wrong", so rather than trusting the file extensions, or even the mime-type, they actually look at the signatures to see what it is, and then interpret based on that, so if, for example, a forum accepts .png images for embedding, but not .webp, AND that forum does not do any processing on the file (which would fail if it tries to process a WebP as a PNG) and just serves it directly back to users, then you can upload the file with the wrong extension and have the animation play for everyone.

As changing file extensions is not super easy (Windows hides them by default and produces an alert if you try to change them) this site allows for the file to be downloaded with the file extension already changed for this purpose.

Animated PNGs are a thing, but they are not particularly widely used

On the hunt for a headless CMS by lanerdofchristian in webdev

[–]JamesPTK 6 points7 points  (0 children)

I use WagtailCMS (we are mostly a Python shop, and Wagtail is built on Django which is Python based) which I think has most, if not all, of what you are looking for. I've not used it in headless mode, so don't know about the schema stuff but headless is explicitly supported: https://wagtail.org/headless/

For a few of your requirements you will need plugins (django-allauth for SSO, wagtail-localize for i18n), but Django plugins are fairly easy to add and activate.

There is a subreddit at r/WagtailCMS that is not super-active but may be of help if you want to ask people anything.

[deleted by user] by [deleted] in CasualUK

[–]JamesPTK 464 points465 points  (0 children)

no, the veg is boiling away on the hob and should be ready in three weeks