Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match and extract if found
#20
    mylist = ip.readlines()    
    myset1 = set(mylist)
Could be refactored to:

    myset1 = set(ip)
An open file-objects has the __iter__ method, which yields line by line, but the ending is not stripped away.
Same with the readlines method of a file-object.

Code with Paths should use pathlib.Path
path2txt = '/home/pedro/myPython/random/'
 
with open(path2txt + 'ip1.txt', 'w') as ip1:
With Path:
from pathlib import Path


path2txt = Path('/home/pedro/myPython/random/')
 
#with open(path2txt + 'ip1.txt', 'w') as ip1:

# then 3 possible ways to use it
with path2txt.joinpath('ip1.txt').open("w") as ip1:
    ...

with (path2txt / 'ip1.txt').open("w") as ip1:
    ...

text_file = path2txt / 'ip1.txt'
with text_file.open("w") as ip1:
    ...
https://realpython.com/python-pathlib/

The same function has an additional issue.

with open(path2txt + 'ip2.txt', 'w') as ip2:
    ipstring = ''
    for i in range(1000):
        ip = makeIP()
        ipstring = ipstring + ip    # <- here the str gets longer and longer and longer.....
    ip2.write(path2txt + ipstring)
This is very memory efficient because a str is immutable.
What really happens behind the scene, is the creation of a new str, where all substrings can fit.
Once ipstring + ip is evaluated, the old ipstring is still in memory and the resulting new str is in memory.
Then the new str is assigned to ipstring.
The old str, where ipstring pointed before, is ready for garbage collection.

Use the str.join method to reduce the memory footprint.
ips = []
for i in range(1000):
    ips.append(makeIP())
ipstring = "\n".join(ips)
To save even more memory:
for i in range(1000):
    ip1.write(makeIP())
The make_ip function could be simplified:
def make_ip():
    return ".".join(str(random.randint(0, 255)) for _ in range(4)) + "\n"
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Match and extract if found - by Calli - Sep-07-2022, 05:00 PM
RE: Match and extract if found - by Gribouillis - Sep-07-2022, 06:27 PM
RE: Match and extract if found - by menator01 - Sep-07-2022, 06:33 PM
RE: Match and extract if found - by Calli - Sep-08-2022, 03:49 AM
RE: Match and extract if found - by perfringo - Sep-08-2022, 04:08 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 08:04 AM
RE: Match and extract if found - by DeaD_EyE - Sep-08-2022, 08:30 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 09:13 AM
RE: Match and extract if found - by DeaD_EyE - Sep-08-2022, 11:45 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 05:54 PM
RE: Match and extract if found - by DeaD_EyE - Sep-09-2022, 09:46 AM
RE: Match and extract if found - by Calli - Sep-12-2022, 06:08 AM
RE: Match and extract if found - by DeaD_EyE - Sep-12-2022, 06:37 PM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:47 AM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:51 AM
RE: Match and extract if found - by Gribouillis - Sep-13-2022, 06:54 AM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:56 AM
RE: Match and extract if found - by DeaD_EyE - Sep-14-2022, 08:37 AM
RE: Match and extract if found - by Pedroski55 - Sep-14-2022, 09:32 AM
RE: Match and extract if found - by DeaD_EyE - Sep-14-2022, 11:27 AM
RE: Match and extract if found - by Pedroski55 - Sep-16-2022, 06:05 AM
RE: Match and extract if found - by Gribouillis - Sep-16-2022, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,439 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  If match not found print last line tester_V 2 3,020 Apr-26-2021, 05:18 AM
Last Post: tester_V
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,676 Nov-23-2020, 05:15 PM
Last Post: cananb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020