Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Methanar
Sep 26, 2013

by the sex ghost
Regex question:

I have the following bit of code that returns some json. I want to change the output to be <whatever>.sub.example.com. Normally I would be able to do this in awk, but in this particular case it needs to be handled in python and I'm not as good at python as I thought. How could I replace example.com with sub.example.com for all the dictionary entries? The filterIP bit works so I was trying to do that again, but I'm having a hard time with it.
code:
def filterIP(fullList):
   regexIP = re.compile(r'\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$')
   return filter(lambda i: not regexIP.search(i), fullList)

def filterSub(fullList2):
   regexSub = re.compile(r'example.com, sub.example.com')
   return filter(lambda i: regexSub.search(i), fullList2)

groups = {key : filterSub(filterIP(list(set(items)))) for (key, items) in groups.iteritems() }

print(self.json_format_dict(groups, pretty=True))

  "role_1": [
    "type-1.example.com",
    "type-12-sfsdf-453-2.example.com"
  ] 

Adbot
ADBOT LOVES YOU

Methanar
Sep 26, 2013

by the sex ghost
I'm not actually working with strings. It's a list.

code:
def filterSub(fullList2):
     return re.sub(r'example\.com$', 'sub.example.com', fullList2)

groups = {key : filterSub(filterIP(list(set(items)))) for (key, items) in groups.iteritems() }

print(self.json_format_dict(groups, pretty=True))

Traceback (most recent call last):
  File "./chef_inventory.py.blah", line 209, in <module>
    main()
  File "./chef_inventory.py.blah", line 205, in main
    ci.execute()
  File "./chef_inventory.py.blah", line 196, in execute
    self.list_nodes()
  File "./chef_inventory.py.blah", line 181, in list_nodes
    groups = {key : filterSub(filterIP(list(set(items)))) for (key, items) in groups.iteritems() }
  File "./chef_inventory.py.blah", line 181, in <dictcomp>
    groups = {key : filterSub(filterIP(list(set(items)))) for (key, items) in groups.iteritems() }
  File "./chef_inventory.py.blah", line 179, in filterSub
    return re.sub(r'example\.com$', 'sub.example.com', fullList2)
  File "/usr/lib/python2.7/re.py", line 155, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer
It's a pretty big one too and I read somewhere that when you need to apply a regex multiple times (assuming I need a regex) I should use re.compile. I don't NEED to use a regex, I just thought that was the right way of doing this sort of thing.

Methanar fucked around with this message at 23:48 on Apr 14, 2017

Methanar
Sep 26, 2013

by the sex ghost
The data is actually more structured like this. I gave the replace snippet a shot, but it didn't do anything.

code:
{ 
 "1": [
    "2.example.com",
    "2.example.com"
  ],
  "2": [
    "2.example.com"
  ],
  "3": [
    "3.example.com",
    "4.example.com"
  ]
} 
Now what I'm thinking is the json output is actually a string (I think), so maybe I can do my string manipulation after writing to json, but that's not going so well either.

code:
 
        groups = {key : filterIP(list(set(items))) for (key, items) in groups.iteritems() }

        s = self.json_format_dict(groups, pretty=True)
#       print(s)

        def filterSub(fullList2):
        return re.sub(r"example.com$", "sub.example.com", fullList2)

        print(filterSub(s))

Methanar
Sep 26, 2013

by the sex ghost
Got it working as it's supposed to. I was being dumb.

This is exactly what I was looking for. Thanks a lot. I learned a few things today.

code:
s_replaced = {key : [domain.replace('example.com', 'sub.example.com') for domain in val] for key, val in s.iteritems()} 

Methanar
Sep 26, 2013

by the sex ghost

funny Star Wars parody posted:

Yeah probably, but that would be significantly more work than my pay grade (free) haha

Actually this bit would be super easy to do.

apt-get install haproxy

then drop in something like this

code:

listen http-frontend
  bind 0.0.0.0:443
  mode http
  default_backend pi-backend

backend pi-backend
  mode http
  server RaspberryPi 10.10.10.10:81
#Comcast IP 

Methanar
Sep 26, 2013

by the sex ghost
Just use bash for windows, why worry at all about powershell?

Methanar
Sep 26, 2013

by the sex ghost
Is there any easy way to change some simple json to ini format? I can't find anything, but I'd like to avoid having to try and figure out how to roll my own converter if necessary.
code:
{
  "key1": [
    "value1",
    "value2",
    "value3"
  ],
  "key2 ": [
    "value4
    "value5"
  ]
}
to
code:
[key1]
value1
value2
value3

[key2]
value4
value5

Methanar
Sep 26, 2013

by the sex ghost
Got it working, thanks for the suggestion.

The important bit
code:
            ini = open(self.ini_path, 'w')
            for key,val in groups.iteritems():
              ini.write('[' + key + ']')
              ini.write('\n')
              ini.write('\n'.join(val))
              ini.write('\n')
            ini.close()
I'm sure this is garbage code, but it works.

Methanar
Sep 26, 2013

by the sex ghost

Loezi posted:

In other words, this needs to work for non-technical users, so a workflow of "start server, go to localhost:8080, quit server when done" is too complicated. And due to <reasons> (mainly: this things does long and heavy computations) dedicated hosting is not an option.

Can't you just roll these into a bash/powershell file to make it single click?

Or this

https://docs.python.org/3/library/webbrowser.html

Methanar
Sep 26, 2013

by the sex ghost
Maybe assign each person an array of weights where the index of each element relates to one of the jobs and the value of the weight relates to a preference for the job.

Then run your favorite sorting algorithm to arrange everyone such that, overall, the greatest ratio of weight-preference being met is achieved.

Then post sorting, do a few overrides to make sure the pairing constraint is met.

Methanar
Sep 26, 2013

by the sex ghost
Look into smokeping, or sensu or any other monitoring system.

This isn't a wheel that needs to be reinvented. You get a lot of free stuff like integration to pagerduty, or nice clicky UIs by using an existing project that does exactly what you're trying.

Methanar
Sep 26, 2013

by the sex ghost
I convert yaml to json so I can actually read it

Methanar
Sep 26, 2013

by the sex ghost
Mongo and javascript are really cool.

We have some data in a collection that is both a string and object, depending on which platform the user was on when the data was created because mongoDB lets you Move Fast and gently caress Up Your Data and the nodejs mongo driver lets it work by accident anyway.

By the way go gently caress yourself if anything else needs to ever access the data

Methanar
Sep 26, 2013

by the sex ghost
A database whose entire appeal that lets you Move Fast and Break Things and Accelerate Developer Velocity and be Agile by letting you get away in the early days with having zero schema, plan, or interdeveloper communication about what the gently caress you're even doing is really cool and good

Methanar
Sep 26, 2013

by the sex ghost
if I can't grep it. Its garbage

Adbot
ADBOT LOVES YOU

Methanar
Sep 26, 2013

by the sex ghost

Empress Brosephine posted:

So I finished Python Crash Course and loved it; what should I read next to improve my skills?

Get a job using it.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply