Jython How to remove characters a string - python

This is a WebSphere related question.
I am trying to turn this command into variables
AdminConfig.modify('(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1324400045826)'
I've found that this command:
AdminConfig.list('J2EEResourceProperty', 'URL*cam_group*)').splitlines()
Will return:
['URL(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1324400045826)', 'URL(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1355156316906)']
So I turned that command into a variable:
j2ee = AdminConfig.list('J2EEResourceProperty', 'URL*cam_group*)').splitlines()
And i'm able to get the string that I want by typing "j2ee[0]" I get
'URL(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1324400045826)'
So that is exactly what I wanted, minus the URL part in the front. How can I get rid of those characters?!

I'm not sure if I understood your requirement, but it seems to me that you want to modify some attributes of J2EEResourceProperty object.
If this is the case, then you don't need to remove that "URL" string, actually you shouldn't do that. The string 'URL(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1324400045826)' fully identifies WebSphere configuration object. Try this:
AdminConfig.modify('URL(cells/taspmociias204Cell01/clusters/cam_group|resources.xml#J2EEResourceProperty_1324400045826)', [['value', 'the new value'], ['description', 'the new description']])
BTW: you can also try using WDR library (https://github.com/WDR/wdr/). Then your script would look as follows:
prop = listConfigObjects('J2EEResourceProperty')[0]
prop.value = 'the new value'
prop.description = 'the new description'
Disclosure: I'm one of WDR contributors.

You could always use a simple replace regular expression to parse out the URL part.
For example:
import re
mystr = 'URL(blahblahblah)'
re.sub(r'^URL', "", mystr)
This is a handy tool to learn and test your regular expressions to make sure they are correct.
http://gskinner.com/RegExr/

Related

Replace words from an API in python

I am using an API I found online for one of my scripts, and I am wondering if I can change one word from the API to something else. My code is:
import requests
people = requests.get('https://insult.mattbas.org/api/insult')
print("Welcome to the insult machine!\nType somebody you want to insult!")
b = input()
print(people.replace("You", b))
Is replace not a command? If so, what plugin and/or commands would I need to do it? Thanks!
The value returned from requests.get isn’t a string, it’s a response object and that class has no replace method.
Have a look at the structure of that class. For example, you can do r = requests.get(...) and r.text.replace(...).
In other words, you need to operate on the text part of the response object.

Python for maya: Why can't I use a variable in concatination with a wildcard?

I'm trying to use the "ls" python command in maya, to list certain objects with a matching string in the name in concatination with a wildcard.
Simple sample code like this:
from maya.cmds import *
list = ls('mesh*')
This code works and will return a list of objects with the matching string in the name, however, I would like to use a variable instead of hard coding in the string. More like this:
from maya.cmds import *
name = 'mesh'
list = ls('name*')
OR like this:
from maya.cmds import *
name = 'mesh'
list = ls('name' + '*')
However, in both examples, it returns an empty list unlike the first. I'm not sure why this is the case because in those examples, the string concatination should come out to 'mesh*' like the first example. I couldn't find an answer on this website, so I chose to ask a question.
Thank you.
JD
PS. If there is a better way to query for objects in maya, let me know what it's called and I'll do some research into what that is. At the moment, this is the only way I know of how to search for objects in maya.
As soon as you add quotes around your variable name like this 'name', you are actually just creating a new string instead of referring to the variable.
There are many different ways to concatenate a string in Python to achieve what you want:
Using %:
'name%s' % '*'
Using the string's format method:
'{}*'.format(name)
Simply using +:
name + '*'
All of these will yield the same output, 'mesh*', and will work with cmds.ls
Personally I stick with format, and this page demonstrates a lot of reasons why.

How to access PyNode from a PyNode list?

I ran this example code with 2 polyCubes in scene.
import pymel.core as pymel
pymel.select('pCube1', 'blinn1')
print pymel.ls(sl = True)
print pymel.ls(sl = True)[0]
and this is my output
[nt.Transform(u'pCube1'), nt.Blinn(u'blinn1')]
pCube1
I know the elements inside this list are PyNodes, but printing them gives out a string type name of the node. Is there anyway to access the PyNode directly from this list?
Found the answer myself.
So apparently the Script Editor returns a representation of PyNode when we print it. Like it's an overloaded str. It is still a PyNode but looks like a string only in Maya's Script Editor. To make it actually appear like a PyNode, we have to use repr() or enclose in back-ticks (`)
Here is the link where I found the answer.
: http://download.autodesk.com/us/maya/2011help/pymel/tutorial.html
Formatting: Read Me First to Avoid Confusion section

MongoAlchemy regex return nothing

I'm testing MongoAlchemy for a project and I've to search user by name.
I'm trying to make a regex but query result is always empty.
I tried two methods :
import re
users = User.query.filter({"name":re.compile("/a/", re.IGNORECASE)}).all()
And :
users = User.query.filter(User.name.regex('/a/', ignore_case=True)).all()
Even if I use a very general regex like /.*/, the result is always empty.
Thank you.
In python regular expressions are not defined using /regexp/, this is javascript syntax.
The proper way to initialize regular expressions would be:
re.compile(r".*", re.IGNORECASE)
So you should use:
users = User.query.filter({"name": re.compile(r".*", re.IGNORECASE)}).all()

how do I modify a url that I pick at random in python

I have an app that will show images from reddit. Some images come like this http://imgur.com/Cuv9oau, when I need to make them look like this http://i.imgur.com/Cuv9oau.jpg. Just add an (i) at the beginning and (.jpg) at the end.
You can use a string replace:
s = "http://imgur.com/Cuv9oau"
s = s.replace("//imgur", "//i.imgur")+(".jpg" if not s.endswith(".jpg") else "")
This sets s to:
'http://i.imgur.com/Cuv9oau.jpg'
This function should do what you need. I expanded on #jh314's response and made the code a little less compact and checked that the url started with http://imgur.com as that code would cause issues with other URLs, like the google search I included. It also only replaces the first instance, which could causes issues.
def fixImgurLinks(url):
if url.lower().startswith("http://imgur.com"):
url = url.replace("http://imgur", "http://i.imgur",1) # Only replace the first instance.
if not url.endswith(".jpg"):
url +=".jpg"
return url
for u in ["http://imgur.com/Cuv9oau","http://www.google.com/search?q=http://imgur"]:
print fixImgurLinks(u)
Gives:
>>> http://i.imgur.com/Cuv9oau.jpg
>>> http://www.google.com/search?q=http://imgur
You should use Python's regular expressions to place the i. As for the .jpg you can just append it.

Categories