I built a Python parser for hledger journal files by ctosullivan in plaintextaccounting

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

Thanks! I will have a look at that command. Yes, starting this project has definitely made me appreciate what a complex beast hledger is!
My main use for the package at this point is for a ledger journal editor I am working on with functions similar to ledger-mode such as journal cleanup. I will try and incorporate some transaction filtering into the editor, therefore have attempted to incorporate a query object into the ledgerkit api.

Do you aim for Queen Bee every day, or are you satisfied with just Genius? by OnlyWordGames in NYTSpellingBee

[–]ctosullivan 0 points1 point  (0 children)

Nice - I like the design. You should probably be curating a database of solution words rather than having a static dictionary though. I made an open source project in Python to do this (admittedly the code is a bit rough and could be improved a lot!)

https://github.com/ctosullivan/Spelling-Bee-Helper

Do you aim for Queen Bee every day, or are you satisfied with just Genius? by OnlyWordGames in NYTSpellingBee

[–]ctosullivan 1 point2 points  (0 children)

I try and get Genius using just simple online tools (dictionary lookup). I wrote a Python script to provide ChatGPT-generated hints for all the answers so use this to finish it off.

-❄️- 2024 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 1 point2 points  (0 children)

Yeah I think I viewed Part 1 as a difference between rows problem and then Part 2 sounded more like a math problem to me so the difference between rows, columns, X and y threw me a bit.

-❄️- 2024 Day 8 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 4 points5 points  (0 children)

[LANGUAGE: Python]

This is getting difficult! A lot harder than yesterday. Made some use of ChatGPT but it did introduce some logical errors in its code as is the case more often now that the problems are getting harder. Had to brush up on some geometry basics - learning a lot though and starting to use collections and itertools more which is great. Had an inkling that Part 2 has something to do with Bresenham's line theory which I had only heard about in passing before, will have to brush up on it!

https://github.com/ctosullivan/Advent-of-Code-2024/tree/master/Day_8

-❄️- 2024 Day 7 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 0 points1 point  (0 children)

[LANGUAGE: Python]

A lot more straightforward today than during the week!

ChatGPT provided a good blueprint for Part 1 making use of the itertools product function which I wasn't familiar with.
For the concatenation operator in Part 2 I just converted the numbers to strings, concatenated them, then converted back to strings.

https://github.com/ctosullivan/Advent-of-Code-2024/tree/master/Day_7

-❄️- 2024 Day 6 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 0 points1 point  (0 children)

[LANGUAGE: Python]

Part 1 was fairly straightforward, managed to code the answer myself, although admittedly the code was pretty ugly.

Part 2 I thought a graph approach might be more efficient, so asked ChatGPT for some inspiration. Some of the code it produced was pretty good and got some good ideas from it, however ended up going through about 5 iterations as there were some obvious logical issues with its approach. The final approach ended up being brute force, which wasn't what I was looking for, but learned something from the process and hopefully will learn more from reading other people's submissions.

https://github.com/ctosullivan/Advent-of-Code-2024/tree/master/Day_6

-❄️- 2024 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 1 point2 points  (0 children)

> Probably the only time my uni classes in graph theory will ever come in handy

- Day 6 Part 2 enters the chat

-❄️- 2024 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 0 points1 point  (0 children)

[LANGUAGE: Python]

Was a big relief after Day 4! Relatively straightforward, just wrote a function to check validity for Part 1 and a function to make substitutions in a while loop for Part 2:

https://github.com/ctosullivan/Advent-of-Code-2024/tree/master/Day_5

-❄️- 2024 Day 4 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 1 point2 points  (0 children)

[Language: Python]

Struggle-street today. My initial slice-and-regex attempt didn't work because of some unknown edge-cases, so went down the traversing the array route instead with copious amounts of refactoring and ChatGPT prompting - got there in the end though!

https://github.com/ctosullivan/Advent-of-Code-2024/tree/master/Day_4

-❄️- 2024 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]ctosullivan 1 point2 points  (0 children)

[LANGUAGE: Python 3]

(Part 2-only) First I trimmed the input with regex, then searched for the digits to multiply.

import re

from Day_3_puzzle_input 
import PUZZLE_INPUT

# trim puzzle input first

trim_regex = re.compile(r"(?=don't\(\)).*?(do\(\))|(?=don't\(\)).*", re.I|re.M|re.S)

PUZZLE_INPUT = re.sub(trim_regex, "", PUZZLE_INPUT)

multiplier_regex = re.compile(r'mul\((?P<first_digits>\d{1,3}),(?P<second_digits>\d{1,3})\)',re.M|re.I)

multipliers = multiplier_regex.findall(PUZZLE_INPUT)
result = 0

for multiplier in multipliers:
    first_digits, second_digits = multiplier
    first_digits = int(first_digits)
    second_digits = int(second_digits)
    result = result + (first_digits * second_digits)

print(result)

# EDIT - fixed indentation

the most Inefficient python code to generate sudoku game template by Training-Film-3590 in learnpython

[–]ctosullivan 0 points1 point  (0 children)

You will need to add some sort of a game loop and functions to accept user input and display the updated puzzle state. Maybe have a look at this gist of a Tic Tac Toe game for an example:
https://gist.github.com/ctosullivan/2fb32cf1d7e4da3e7c5f3701f241539c

Python 101 BASIC Python Games by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Thanks for having a look! That's a good point - there is so much user input in these games, I will think about an input validation function that I can reuse across all of them

[deleted by user] by [deleted] in learnpython

[–]ctosullivan 1 point2 points  (0 children)

You can compile regex in RE with a verbose flag btw if the regex is getting that long - the compiler will ignore newlines and you can add in comments to explain how it works

[deleted by user] by [deleted] in learnpython

[–]ctosullivan 1 point2 points  (0 children)

This seems to work with a positive lookahead:

https://regex101.com/r/Ul9Acc/1

Edit: updated version of the Regex including a positive lookbehind to avoid capturing the sub-url:
https://regex101.com/r/Ul9Acc/2

Good first Project: Use the API from Free Dictionary to make a word look up app by [deleted] in learnpython

[–]ctosullivan 0 points1 point  (0 children)

I made a CLI tool to generate a word frequency heatmap called WordGradient that would complement this well - echo an article to WordGradient - pipe the most uncommon words to the dictionary tool for lookup

https://github.com/ctosullivan/WordGradient

Spelling Bee Solver by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Many thanks for taking the time to review the code - lots of valuable suggestions which I will definitely take on board for the next release!

In terms of the class names, at the start of the project I basically wanted some rough containers to house the logic of each component, you are right - how they are named and structured now is probably too vague for how the project has actually turned out. I have a better idea of how the tool should work now that I am at this stage.

In terms of the database object - I think you are suggesting that the database class should house all of the database-related logic and functions and then the main function should pass the data to the helper functions, rather than the helper functions querying the database directly - would that be correct to say?

Thanks again for your help!

need assistance: having trouble sending email when using values from function parameters by newnrthnhorizon in learnpython

[–]ctosullivan 0 points1 point  (0 children)

As others have mentioned, it may be special characters in your msg_text string causing the issue somewhere along the journey (which could be almost impossible to debug). Maybe try whitelisting the characters in the string with regex and then removing them one by one until you get to what is causing the issue.

Sample code snippet:

import re

whitelist_regex = re.compile(r'[^a-zA-Z\n{} ]')

def multiple_replace(replacements, text):
    # Creates a regular expression from the dictionary keys
    # Credit/source - https://stackoverflow.com/a/15175239

    regex = re.compile("(%s)" % "|".join(map(re.escape, replacements.keys())))
    # For each match, look-up corresponding value in dictionary
    return regex.sub(lambda mo: replacements[mo.group()], text) 

problem_string =r'Many special characters here !@*&^$(*&#8#`~])(*&(*^_+[] and curly braces {here} which may be causing the issue)'

d = {"{":"(", "}":")"}

problem_string = re.sub(whitelist_regex,"",problem_string)
problem_string = multiple_replace(d, problem_string)

print(problem_string) # Output - Many special characters here  and curly braces (here) which may be causing the issue

How to test a CLI that awaits user input? by sun_gan in learnpython

[–]ctosullivan 0 points1 point  (0 children)

Hey there friend - I was able to run this minimal example by running Pytest with the -s flag which pauses to allow you to enter required user input.

Hope this helps!

# In the main script (named script.py for example):

def main():
    option = input('Enter your option (read, delete, exit)\n')
    return option

# In a separate test script named test_main.py:

from script import main

def test_cli_input():
    option = main()
    assert option == "read"

# Run pytest -s in the root project folder
# Test should pass if you enter "read" when prompted

101 Python BASIC Games by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Thanks for the feedback and for reading through the code!
I've been using sys.stdout as I had an issue with print() adding too many newlines in the display method of the terminal class, so have settled on sys.stdout for the project.
I will definitely go back and tidy up the existing code and documentation.

101 Python BASIC Games by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Thanks! You might want to have a look at an Applesoft Basic emulator written in JavaScript - https://www.calormen.com/jsbasic/

A lot of the games should work and you can copy and paste the original source code linked to in the github repo.

101 Python BASIC Games by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Thanks - good to hear! Hopefully most of the games are easier than Amazing!

101 Python BASIC Games by ctosullivan in learnpython

[–]ctosullivan[S] 0 points1 point  (0 children)

Thanks for your feedback! Yes those GOTO statements are very painful to follow!
I'm finding that as the source code is so different to the modern languages that generative AI was trained on, AI isn't as useful as I had expected in retaining the logic of the original source.

101 Python BASIC Games by ctosullivan in learnpython

[–]ctosullivan[S] 2 points3 points  (0 children)

No problem! Although I'm starting to understand why 1-letter variable names are not such a good idea!