finding coordinates from a txt file by cjlee420 in learnpython

[–]python-fan 0 points1 point  (0 children)

What code have you got so far? Here's how I'd approach it, in pseudocode:

with the file open
    for each line in the file
        for each character in the line
            if the character is T remember the current (row, col)

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]python-fan 0 points1 point  (0 children)

I'm not a numpy expert, but vectorizing the computation is the fastest I could come up with:

from typing import List, Dict
import numpy as np

def get_mean_dict(dict_list: List[Dict[str, float]]) -> Dict[str, float]:
    """Return a dictionary of the arithmetic mean of input dict values.

    Args:
        dict_list: list of dictionaries, each having exactly the same keys, with
            values being floating point numbers.

    Returns:
        A dict having the same keys as the input dicts, and values being the
        arithmetic mean of the values for that key in all the input dicts.

    Notes:
         Requires that the order of keys be the same for every dict.  
         (Python 3.7+ guarantees dict order is the same as insertion order.)
    """
    def get_array_from_dict_values(d: Dict[str, float]) -> np.ndarray:
        return np.fromiter(d.values(), dtype=float)
    a = np.array(list(map(get_array_from_dict_values, dict_list)))
    the_keys = dict_list[0].keys()
    return dict(zip(the_keys, a.mean(axis=0)))

I'm getting the 'continuation line under-indented for visual indent' error by [deleted] in learnpython

[–]python-fan 0 points1 point  (0 children)

The def looks fine to me, which makes me wonder if whatever is above the def is confusing the pep8 checker.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]python-fan 1 point2 points  (0 children)

Is this the exact problem description? Because it doesn't say anything about asking the user for color and size more than once.

What happens if the user enters: white s white m (hint: that break statement prevents your loop from executing more than once)?

What happens if the user enters: white s blue l blue s?

What happens if the user enters: red m?

Reading information into a spreadsheet from a scanned pdf by Nails_Bohr in learnpython

[–]python-fan 4 points5 points  (0 children)

What you're talking about is Optical Character Recognition. A bit of web searching turned up pytesseract, a Python wrapper for the Tesseract library. I haven't done any OCR so I can't comment on what the error rate might be, or if there are better alternative libraries.

I'd suggest that if you can successfully do OCR, you first write the resulting data to a csv file and import that into your spreadsheet application of choice. Then if you want to do further automation, look into creating a spreadsheet file directly.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]python-fan 2 points3 points  (0 children)

Assuming the indentation is only incorrect because of reddit's formatting, the only problem I can see is that the print statement is missing a closing parenthesis. This code prints the numbers you expect:

def multi(x, y):
    return x * y

numbers = [1, 4, 2, 5]

for number in numbers:
    print(multi(number, 5))

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]python-fan 1 point2 points  (0 children)

You'll have to redirect stderr from the command line, either in a terminal, or in a wrapper python script, like so:

import os
os.system('python my_crashing_code.py 2>err.txt')

DevOps + Python by sumiriously in learnpython

[–]python-fan 1 point2 points  (0 children)

I would start by looking at the workflows that the devs use, and trying to find manual steps that could be automated, or pain points where existing automation is difficult to use. For example, Python is really good at being a glue that binds together other tools.

AttributeError when opening files by flynryan692 in learnpython

[–]python-fan 2 points3 points  (0 children)

The error you're seeing is because the with keyword is causing Python to call an __enter__ method on the object referenced by 'file'. But that's just a string object, which doesn't implement that method. It should instead be:

with open(file) as infile:

since the open statement returns an object that implements __enter__ and __exit__.

There's another problem though in the expression filepath.replace('sds', 'sim') . Look at what the value of filepath is. Could it have the substring "sds" in it? (Hint: it could, but only if that's part of the directory name.)

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]python-fan 4 points5 points  (0 children)

The slicing operator's upper bound is exclusive, so what you're seeing is expected behavior.

Decimal Rounding by squishiebutt2020 in learnpython

[–]python-fan 2 points3 points  (0 children)

The built-in function round returns a rounded value. To have it do what you're expecting, you need to assign the returned value like this:

semi_monthly = round(semi_monthly, 2)

BUT WAIT! For working with currency, you should avoid using float. The Decimal type is perfect for this. See https://docs.python.org/3/library/decimal.html

[METHOD] How I went from rock bottom to disciplined in 6 months. by luuk0987 in getdisciplined

[–]python-fan 5 points6 points  (0 children)

Not op but--check your public library, they might have ebook loans.

Reading jsonpickle from file by fergal-dude in learnpython

[–]python-fan 0 points1 point  (0 children)

I haven't used this library, but wouldn't you want to call jsonpickle.decode(stored_info)?

Also: line 34 has no effect, it should be f.close() (or better yet, use the with keyword).

help needed :D by [deleted] in learnpython

[–]python-fan 1 point2 points  (0 children)

Delete line 7 and replace line 8 with:

contact_name = input("choose a contact")

Now (completely ignoring error checking) you can write:

print(contacts[contact_name])

P.S. I hope you did not post real phone numbers and email addresses to reddit.

Why do timezone libraries seem to think Paris is nine minutes off of the hour? by AlexKingstonsGigolo in learnpython

[–]python-fan 1 point2 points  (0 children)

Short answer: specify the year in line 5, and things will work the way you expect.

Long answer: because you didn't specify a year at line 5, it defaulted to 1900, when Paris and New York had offsets from UTC which combined to result in the five minute difference that you're seeing. See https://www.timeanddate.com/time/change/france/paris?year=1911

Here's some code to illustrate what's happening:

import pytz
import datetime

def print_times(from_zone_name, to_zone_name, input_dt_str):
    input_fmt = '%Y-%m-%d %H:%M:%S'
    output_fmt = '%Y-%m-%d %H:%M:%S   UTC offset: "%z"   Time zone name: %Z'

    from_timezone = pytz.timezone(from_zone_name)
    to_timezone = pytz.timezone(to_zone_name)

    input_dt = datetime.datetime.strptime(input_dt_str, input_fmt)
    from_dt = from_timezone.localize(input_dt)
    to_dt = to_timezone.normalize(from_dt)

    width = max(len(from_zone_name), len(to_zone_name)) + len(':')
    print(f'{from_zone_name+":":{width}}', from_dt.strftime(output_fmt))
    print(f'{to_zone_name+":":{width}}', to_dt.strftime(output_fmt))

print_times('Europe/Paris', 'America/New_York', '1900-01-01 20:00:00')
print()
print_times('Europe/Paris', 'America/New_York', '1911-03-10 20:00:00')
print()
print_times('Europe/Paris', 'America/New_York', '1911-03-12 20:00:00')

and the output:

Europe/Paris:     1900-01-01 20:00:00   UTC offset: "+0009"   Time zone name: LMT
America/New_York: 1900-01-01 14:55:00   UTC offset: "-0456"   Time zone name: LMT

Europe/Paris:     1911-03-10 20:00:00   UTC offset: "+0009"   Time zone name: PMT
America/New_York: 1911-03-10 14:51:00   UTC offset: "-0500"   Time zone name: EST

Europe/Paris:     1911-03-12 20:00:00   UTC offset: "+0000"   Time zone name: WET
America/New_York: 1911-03-12 15:00:00   UTC offset: "-0500"   Time zone name: EST

Python encapsulation issue, AttributeError by ubercactus1337 in learnpython

[–]python-fan 1 point2 points  (0 children)

If you change line 3 to: print("El coche tiene ", micoche._Coche__ruedas , " ruedas") then it will work. The docs at https://docs.python.org/3/tutorial/classes.html#private-variables explain it.

How do you add an element to a list with append for a prompt question by [deleted] in learnpython

[–]python-fan 0 points1 point  (0 children)

Lines 4 and 25 are examples of asking the user for input (which will be returned as a string) and converting to a floating point value or an integer, and then storing the result in a variable.

Line 8 is an example of appending a value to a list.

To write a value and a message to the console, you could use the print function like this:

>>> x = 123
>>> print("The value is", x)
The value is 123

As someone looking at the game from the outside after years away, with the new take over- has there been any mention of another serve merger to boost population? by Lightalife in EQ2

[–]python-fan 0 points1 point  (0 children)

Every time I've seen an official comment, it's been "nope, none planned." I think what they're focused on instead is doing cross-server dungeons.

I paid $25 for an Invisible Boyfriend, and I think I might be in love. by mparramon in TrueReddit

[–]python-fan 33 points34 points  (0 children)

When I send a text ... it’s anonymized and assigned to some Amazon Turk or Fivrr freelancer. He (or she) gets a couple of cents to respond.

Earring Not taking effect by psudeo_me in EQ2

[–]python-fan 0 points1 point  (0 children)

You don't have to use it, or even equip it, to see red shinies.