Simple Field Calculation In ArcPy - arcpy

Can some one please let me know how I can do simple field calculation like Copying From "A" to "B" in ArcPy? I have found lots of examples on the web where they all used extra expressions and they were complicated. What I need is something like ArcMap Gui:
B = !A!
Updated:
Here is the code I have so far but I am getting error on running this as
NameError: name 'A' is not defined
point_shp = "G:\\Temp\\All_Provinces.shp"
arcpy.AddField_management(point_shp, "B", "TEXT", "", "", "25", "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(point_shp, "B", "A", "PYTHON_9.3")

Optionally, if you wanted the calculate field tool to compute using VB then you could do this:
arcpy.CalculateField_management(point_shp, "B","[A]")
That's just if you have an application that calls for visual basic instead.

Ok i got it, the A must be wrapped like "!A!"
arcpy.CalculateField_management(point_shp, "B","!A!", "PYTHON_9.3")
wow this forum is awesome! so many help!

I would attack this by using an update cursor, though I do not know if that meets your need of simplicity.
point_shp = "G:\\Temp\\All_Provinces.shp"
updateCursor = arcpy.UpdateCursor(point_shp)
for row in updateCursor:
a = row.getValue("name of the "a" field name")
b = a # --> any manipulations to b can also go here.. example b = a + c etc...
# --> alternatively if you need to do string manipulations you can always make use of specifiers. such that a = "!%s!" %(a) note:use %d if "a" is a number. This would produce a string "!a!" if you needed it in that format.
row.setValue("name of "b" field", b)
updateCursor.update(row)
The above will allow you to get values of field "A", do something to those values if need be and store as b. Then populates field "B" with b.
Note: If field B is not already created you will need to do so...

Related

Can a value from Groovy map be self referenced for interpolation purposes?

I want to know if we can interpolate a value within a Groovy map using it's other Key-Value entries. For example,
def ten = 10
def example = [
a: "apple",
b: "${ten} bats",
c: "${example.b}" //or something like self.b
]
If so, please let me know how to get this done?
As stated already in the comments, the literal for maps most likely does not allow that. But you can come very close to your code there with tap (or with if you are <2.5)), which allows to lookup things inside the map (basically this is the self reference you where looking for)
E.g.
def ten = "ten"
def map = [:].tap{
a = "apple"
b = "${ten} ${a}s"
}
assert map == [a:"apple", b:"ten apples"]

Parsing program config files

I need to write reader class for a program config file, and I'm trying to determine the best way to approach this.
I have some experience with boost::spirit (actually, just enough to know that I am terrible at writing grammars), and a good bit of experience with regular-expressions.
Other than that, I can't think of much. So my options seem to be:
boost::spirit
std or boost regex
ol' fashion string parsing
I am looking for suggestions or advice on how to proceed - or even pseudo-code should anyone feel inclined.
I realize this is a bit of an opinion-based question, but I'm really struggling with writers-block here. I'm worried I'll start down a path, and waste a lot of time before realizing it was a poor choice.
The config file format is already defined, and looks like this:
Group1 {
Scalar1 = 500
Scalar2 = 45.5
Scalar3 = My Value
List1 {
LS1 = 123
LS2 = hello world
}
List2 {
LS1 = 456
LS2 = goodbye world
}
Array1 [
300
200
25
]
Array2 [
true
false
true
false
]
};
The config will always have at least one Group.
A Group will contain 0..n Scalar, List, and/or Array entries
A scalar is: label = value
A List is a labeled container of Scalars
An Array is a labeled container of values
The labels shown (eg. Scalar1, Scalar2, ...) are just examples. They can be called anything: maxrate = 500, avg_val = 45.5, would have been just as valid.
Copy the file to tmp, replacing every instance of = with :, then read it as json with boost::ptree.

algorithm for permutations of a list with specific orders for certain variables

I wanted to ask if there was a way to use an algorithm or some program to actually get a list back with all the permutations of a sequence of specific variables, but there can only be certain variables in certain places. I'm trying to find a way to create uniquely designed name for something, and for examples' sake, let's use the name "Coda." That would be the way to pronounce it, but I want to use a name like "{ 0 |] 4." Obviously I wouldn't want the "C" to wind up anywhere else. But there are other options than "{" for the "C." For instance, "[ 0 |] 4." And in place of the "0" for "o," I could use "()," as in "{ () |] 4." But I could also use that same "()" in the second example for "C," and it would be "[ () |] 4." But I wouldn't want the things I use for "o" to be anywhere else in the name, like instead of "4," it and the "()" are switched, spelling out "Cado." Not exactly the word I'm looking for. But I'm wondering if there's a way to list out entirely all permutations, with certain things always having to be in a certain place.
These would be like what I would use for all possibilities of the letters, which is why it got so complicated when I started trying to list out all the permutations myself (obviously the commas aren't part of the letters):
C: <, [, {, (
o: 0, (), [], {}
d: |>, |], |},|)
a: 4, /\, /-\, /=\, #
First of all, I should note that permutation is not the correct term: 'coda'->'daoc' is a permutation, and is not what you want to do.
If you want to print 'Coda' with all possible combination of variations for each character, you just need to have four nested cycles, one for each character.
In pseudocode, you would have:
c_chars = ['<', '[', '{', '(']
o_chars = ['0', '()', '[]', '{}']
d_chars = ['|>', '|]', '|}','|)']
a_chars = ['4', '/\', '/-\', '/=\', '#']
foreach c in c_chars{
foreach o in o_chars{
foreach d in d_chars{
foreach a in a_chars{
print(c+o+d+a+'\n') //concatenate the four characters
}
}
}
}
This would print 4*4*4*5=320 lines, each with a different way of spelling 'Coda' with your indicated possibilities.

Storage method for Android App

I'm making an Android App for my friends and family based on the game Taboo. Basically, the game displays a main word, and then a list of restricted words. The object is to get your teammates to say the main word, but you cannot say any of the restricted words.
For this app, I would need several hundred main words, plus 7 restricted words for each main word.
My issue is how to store these so they can be accessed by a randomly by the app.
My initial thought was to use an XML resource using string arrays, but I do not know how I would reference these with a random generator.
I am open to any and all suggestions. I am working in Eclipse using the Android SDK.
Just to clarify, I am experienced in Java/C/C++, but this is my first Android App. I have the game framework almost complete, and this is my only major issue.
Any help is appreciated.
You can use the xml method you said with a random. Something like this:
Lets say you have 1 string-array which contains the 8 words (1 correct and 7 restricted), you can have as many arrays with different names. They are saved in string.xml
Now in java create the Random and check what number you get, then if you get x number open x string-array.
Here's an example:
<string-array name="a">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
<item>G</item>
<item>H</item>
</string-array>
now in java:
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
then check your number:
if (i1 == 0){
//do your magic code here with string-array a
} else ...
or use any method you like, like switch...
Edit: or a trick can be to make names of string-array a# (Of curse # is a number) then do:
array = getResources().getStringArray(R.array.a+i1);
With this method you don't need the if else or switch or whatever you want to use.
NOTE: this code is from my mind so it can have some writing errors.
It seems like you have a data model as such: you have a list of main words. Each main word object contains the word itself and then a list of associated restricted words.
This looks like something you can either do with JSON or XML (or Protocol Buffers, I guess). I'd go with JSON because it has a balance between speed and Android support. You can create the JSON files using an editor, and they can look like this:
[{
"name": "animal",
"restricted": ["cat", "dog", "mouse"]
},{
"name": "fruit",
"restricted": ["kiwi", "apple", "orange"]
},
{
"name": "city",
"restricted": ["Paris", "London", "Madrid"]
}]
Once you open the file and convert it to a String, you can parse it using JSONArray(yourString) and then use the appropriate methods from there to get each individual word JSONObject, its name String and its JSONArray of restricted word Strings.
I developed a quiz app (one question, multiple choice of 5 answers) in which I stored everything in a SQLite database. With this implementation you could also use categories for your words if you wish to. What you can do is get a count of how many words there are and have this as an integer arraylist containing 0 to however many. Then I shuffled the list so it's in a random order. This way I don't repeat any question, unlike when using random which potentially could. Another possibility is to remove the word each time you use it if you do wish to use random. The benefit of this way is also that you don't have to reference each string, string array etc - you could just add more words each time to the database and the fixed structure will deal with it automatically.

String templating

I have the task of designing a feature that generates Id numbers that follow rules that can be extended later.
For example; A system may require their id numbers be generated as follows: 18122424. This would be broken down into: [DepartmentId][YearCreated][Sequential]. Department = 18, Year = 12, Sequence = 2424.
How do I go about designing a rule engine that allows the user to change it? I came up with a format like:
Dept(#)
Year(#)
Seq(#)
Initials(#) <-- Name initials.
So the rule for that Id above is: [Dept(2)][Year(2)][Seq(4)]. If I get this as a string, how do I parse it to get the rules? Regex or normal string search?
Is there an easier or more efficient way of doing this conceptually?
If I am right, your question is about parsing the rule string, i.e. retrieving the field names and lenghts.
I would work one field at a time and possibly use a Regexpr for [ letters ( digits ) ], like
\[{[A-Za-z]+}\({[0-9]+}\)\]
(MSVC syntax, retrieve the two tagged expressions).
You'll also need to store a dictionary of possible field names and convert the digits to an integer.
Alternatively, combined C strchr and scanf can do the trick.
This is not a "rule engine", this is just templating.
Whatever language you are using has some kind of templating, and a way to specify all of these formats. Just use that.
I don't know who upvoted it.
Generating the number
- you first have to decide the template. something like depid-year-seq. Assuming you have only these 3 "Variables". Then you should have have these variables mapped to original value. use a string parser and split the "given" template on -. It will give you array with index 0 = depid, 1 = year, 2 = seq. Loop through the array and create a string by using the corresponding values for each string on given index i.e.
- 0 = depid = "18"
- 1 = year = "18" + "12" = "1812"
- 2 = seq = "1812" + "1212" = "18121212"
Reverse
you should parse your given number by splillting it up in 2-2-rest .. .. i guess you can pick up from here.

Resources