I have following parsing problem. My keywords can be prefixed with an underscore for deactivation of the option, block etc.
#coding: utf8
from pyparsing import Keyword, Combine, pyparsing_common, Literal, Suppress, Group, OneOrMore
test_string = r'''
keyword1list {
keyword1 {
option 213
}
_keyword1 {
option 214
}
}
'''
This can happen to any keyword, here keyword1list, keyword1 or option. What I like to achieve is to either leave those blocks out during parsing or parse them but catch the deactivation prefix.
Currently, I can successfully parse the "activated" test_string with the following code, but it fails for apparent reasons with the underscored keyword.
lparent = Suppress(Literal('{'))
rparent = Suppress(Literal('}'))
kw1_block = Keyword('keyword1') + lparent
kw1_block = kw1_block + Keyword('option') + pyparsing_common.number.setResultsName('option')
kw1_block = Group(kw1_block + rparent).setResultsName('keyw1')
kw2_block = Keyword('keyword1list') + lparent
kw2_block = kw2_block+ OneOrMore(kw1_block) + rparent
kw2_block = Group(kw2_block).setResultsName('keyword1list', listAllMatches=True)
result = kw2_block.parseString(test_string)
print(result.dump())
tmp = kw2_block.runTests(test_string.replace('\n', '\\n'))
print tmp[0]
My current solution is, to put all keywords in a list and set up a dictionary to combine them all with the underscore and give them a flag.
#coding: utf8
from pyparsing import Keyword, Combine, pyparsing_common, Literal, Suppress, Group, OneOrMore, ZeroOrMore
test_string = r'''
keyword1list {
_keyword1 {
option 1
}
keyword1 {
option 2
}
_keyword1 {
option 3
}
keyword1 {
option 4
}
keyword1 {
option 5
}
}
'''
kwlist = ['keyword1', 'keyword1list', 'option']
keywords = {}
for k in kwlist:
keywords[k] = Keyword('_' + k).setResultsName('deactivated') | Keyword(
k).setResultsName('activated')
lparent = Suppress(Literal('{'))
rparent = Suppress(Literal('}'))
kw1_block = keywords['keyword1'] + lparent
kw1_block = kw1_block + keywords[
'option'] + pyparsing_common.number.setResultsName('option') + rparent
kw1_block = Group(kw1_block).setResultsName('keyword1', listAllMatches=True)
kw2_block = keywords['keyword1list'] + lparent
kw2_block = kw2_block + ZeroOrMore(kw1_block) + rparent
kw2_block = Group(kw2_block).setResultsName('keyword1list')
result = kw2_block.parseString(test_string)
print(result.dump())
tmp = kw2_block.runTests(test_string.replace('\n', '\\n'))
print tmp[0]
While this allows to parse everything properly I have to recreate the logic afterwards (finding the deactivated keywords and drop them from the result), which I like to avoid. I believe I need a parseAction on the underscored keywords to drop those tokens somehow but I currently cannot figure out how to do this.
Any help is greatly appreciated.
When I see a parser that is intended for filtering out selected blocks of text, my first approach is usually to write a parser that will match just the selected part, and then use transformString with a suppressed form of that parser:
kwlist = ['keyword1', 'keyword1list', 'option']
to_suppress = MatchFirst(Keyword('_' + k) for k in kwlist)
kw_body = nestedExpr("{", "}") | Word(nums)
filter = (to_suppress + kw_body).suppress()
print(filter.transformString(test_string))
Running this with your test string gives:
keyword1list {
keyword1 {
option 2
}
keyword1 {
option 4
}
keyword1 {
option 5
}
}
Related
I am developing a structure of R6 classes. The idea is to have a big R6 class collecting many small objects of other R6 class types. The big object is the input to a function.
The point is that I would like to make it simple for users to modify the small objects within the big by developing R6 methods in the public object that would facilitate this.
Let me explain on the following example:
ClassOne object is one of the small objects:
> ClassOne = R6::R6Class(
+ "ClassOne",
+
+ public = list(
+ element = numeric(),
+
+ initialize = function() {
+ self$element <- 1
+ },
+
+ set_element = function(value) {
+ if (!missing(value)) {
+ self$element <- value
+ }
+ }
+
+ )
+ )
ClassTwo object is the big object that contains as self$class_one_element an object of class ClassOne.
> ClassTwo = R6::R6Class(
+ "ClassTwo",
+
+ public = list(
+
+ class_one_element = list(),
+
+ initialize = function() {
+ self$class_one_element = ClassOne$new()
+ },
+
+ get_class_one = function() {
+ self$class_one_element$clone()
+ }
+ )
+ )
Once the ClassTwo object is initiated:
> ct = ClassTwo$new()
> ct$class_one_element$element
[1] 1
the method get_class_one is cloning the whole self$class_one_element:
> ct_co = ct$get_class_one()
The method set_element is used to modify the small object:
> ct_co$set_element(2)
> ct_co$element
[1] 2
but despite the application of deep cloning, this operation is modifying only the element in the small object ct_co and not the corresponding element in the big object:
> ct$class_one_element$element
[1] 1
THE QUESTION: How to rewrite the ClassTwo (I guess) so that the sequence:
> ct = ClassTwo$new()
> ct_co = ct$get_class_one()
> ct_co$set_element(2)
is modifying ct$class_one_element$element as well?
I would greatly appreciate your suggestions!
I'm trying to get tf 0.12.x new dynamic feature to work with a nested map, config is below.
As you can see below (simplified for this) I'm defining all the variables and adding variable required_resource_access which contains a map.
I was hoping to use new dynamic feature to create read this map in a nested dyanmic block.
variable prefix {
description = "Prefix to applied to all top level resources"
default = "abx"
}
variable suffix {
description = "Suffix to applied to all valid top level resources, usually this is 2 letter region code such as we (westeurope), ne (northeurope)."
default = "we"
}
variable env {
description = "3 letter environment code appied to all top level resources"
default = "dev"
}
variable location {
description = "Where to create all resources in Azure"
default = "westeurope"
}
variable available_to_other_tenants {
default = false
}
variable oauth2_allow_implicit_flow {
default = true
}
variable public_client {
default = false
}
# other option is native
variable application_type {
default = "webapp/api"
}
variable required_resource_access {
type = list(object({
resource_app_id = string
resource_access = object({
id = string
type = string
})
}))
default = [{
resource_app_id = "00000003-0000-0000-c000-000000000000"
resource_access = {
id = "7ab1d382-f21e-4acd-a863-ba3e13f7da61"
type = "Role"
}
}]
}
variable reply_urls {
default = []
}
variable group_membership_claims {
default = "All"
}
resource "azuread_application" "bootstrap" {
name = "${var.prefix}-${var.env}-spn"
homepage = "http://${var.prefix}-${var.env}-spn"
identifier_uris = ["http://${var.prefix}-${var.env}-spn"]
reply_urls = var.reply_urls
available_to_other_tenants = var.available_to_other_tenants
oauth2_allow_implicit_flow = var.oauth2_allow_implicit_flow
type = var.application_type
group_membership_claims = var.group_membership_claims
dynamic "required_resource_access" {
for_each = var.required_resource_access
content {
resource_app_id = required_resource_access.value["resource_app_id"]
dynamic "resource_access" {
for_each = required_resource_access.value["resource_access"]
content {
id = resource_access.value["id"]
type = resource_access.value["type"]
}
}
}
}
}
But for reasons beyond my knowledge it keeps giving me this error (notice it's priting it twice as well), I've tried a few other options but this is the closest I managed to get where it would at least give me a meaningful error.
------------------------------------------------------------------------
Error: Invalid index
on pe_kubernetes.tf line 24, in resource "azuread_application" "bootstrap":
24: id = resource_access.value["id"]
|----------------
| resource_access.value is "7ab1d382-f21e-4acd-a863-ba3e13f7da61"
This value does not have any indices.
Error: Invalid index
on pe_kubernetes.tf line 24, in resource "azuread_application" "bootstrap":
24: id = resource_access.value["id"]
|----------------
| resource_access.value is "Role"
This value does not have any indices.
Error: Invalid index
on pe_kubernetes.tf line 25, in resource "azuread_application" "bootstrap":
25: type = resource_access.value["type"]
|----------------
| resource_access.value is "7ab1d382-f21e-4acd-a863-ba3e13f7da61"
This value does not have any indices.
Error: Invalid index
on pe_kubernetes.tf line 25, in resource "azuread_application" "bootstrap":
25: type = resource_access.value["type"]
|----------------
| resource_access.value is "Role"
This value does not have any indices.
Spent the best part of 2 days on this with no luck so any help or pointers would be much appreciated!
I had some time to test my comment...
If I change the resource_access to a list it works.
See code below:
variable required_resource_access {
type = list(object({
resource_app_id = string
resource_access = list(object({
id = string
type = string
}))
}))
default = [{
resource_app_id = "00000003-0000-0000-c000-000000000000"
resource_access = [{
id = "7ab1d382-f21e-4acd-a863-ba3e13f7da61"
type = "Role"
}]
}]
}
resource "azuread_application" "bootstrap" {
name = "test"
type = "webapp/api"
group_membership_claims = "All"
dynamic "required_resource_access" {
for_each = var.required_resource_access
content {
resource_app_id = required_resource_access.value["resource_app_id"]
dynamic "resource_access" {
for_each = required_resource_access.value["resource_access"]
content {
id = resource_access.value["id"]
type = resource_access.value["type"]
}
}
}
}
}
And the plan shows:
Terraform will perform the following actions:
# azuread_application.bootstrap will be created
+ resource "azuread_application" "bootstrap" {
+ application_id = (known after apply)
+ available_to_other_tenants = false
+ group_membership_claims = "All"
+ homepage = (known after apply)
+ id = (known after apply)
+ identifier_uris = (known after apply)
+ name = "test"
+ oauth2_allow_implicit_flow = true
+ object_id = (known after apply)
+ owners = (known after apply)
+ public_client = (known after apply)
+ reply_urls = (known after apply)
+ type = "webapp/api"
+ oauth2_permissions {
+ admin_consent_description = (known after apply)
...
}
+ required_resource_access {
+ resource_app_id = "00000003-0000-0000-c000-000000000000"
+ resource_access {
+ id = "7ab1d382-f21e-4acd-a863-ba3e13f7da61"
+ type = "Role"
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
I removed a lot of your variables an some of the optional Arguments for azuread_application to keep the code as small as possible, but the same principle applies to your code, use lists on for_each or it will loop on the object properties.
I am trying to deserialize the following JSON to an array using C# dynamics:
[
{
"itemId":"15",
"quantity":101,
"eventTimestamp":"00000000-0000-0000-0000-000000000000",
"salesChannel":"1",
"unlimitedQuantity":false
},
{
"itemId":"15",
"quantity":101,
"eventTimestamp":"00000000-0000-0000-0000-000000000000",
"salesChannel":"2",
"unlimitedQuantity":false
}
]
I have already tried two different approaches, without success:
dynamic itemsBalance = JObject.Parse(content);
and
var itemBalanceType = new {
itemId = "", quantity = 0, eventTimestamp = "", salesChannel = ""
};
var itemsBalance = JsonConvert.DeserializeAnonymousType(content, itemBalanceType);
I am currently using C# dynamics with all other deserializations, and would not like to create classes for each response.
Is there any way to do this ?
Thanks
I found a solution:
JArray itemsBalance = JArray.Parse(content);
if (itemsBalance != null)
{
for (int i = 0; i < itemsBalance.Count; i++)
{
string itemBalanceJSON = itemsBalance[i].ToString();
dynamic itemBalance = JObject.Parse(itemBalanceJSON);
lbxResponse.Items.Add(itemBalance.itemId + " - " + itemBalance.salesChannel +": " + itemBalance.quantity.ToString());
}
}
Should there be any better, please let me know...
You could do it with less amount of code:
dynamic result = JsonConvert.Deserialize(content);
foreach(var entry in result)
{
lbxResponse.Items.Add(entry.itemId + " - " + entry.salesChannel + ": " + entry.quantity);
}
I am trying to use DateTime in global.asax to give a name to a file but it gives an error. Could you please assist?
The code I am using for the DateTime;
public void callFileCreate()
{
string path = ConfigurationManager.AppSettings["LogFileFolder"].ToString();
string filename = HttpContext.Current.Server.MapPath(path + "\\Log_" + DateTime.Now.ToShortDateString().Replace("/", ".") + "_" + (DateTime.Now.ToLongTimeString()).Replace(":", "_") + ".txt");
TraceFilePath = HttpContext.Current.Server.MapPath(path + "\\Scheduler" + DateTime.Now.ToShortDateString().Replace("/", ".") + "_" + (DateTime.Now.ToLongTimeString()).Replace(":", "_") + ".txt");
FileStream fs = null, fs1 = null;
fs = File.Create(filename);
fs1 = File.Create(TraceFilePath);
ErrorFilePath = filename;
}
You should use the Path class if you work with paths:
string path = ConfigurationManager.AppSettings["LogFileFolder"].ToString();
string fileName = string.Format("{0}_{1}_{2}.txt"
, "Log"
, DateTime.Today.ToString("dd.MM.yyyy") // change according to your actual culture
, DateTime.Now.ToString("HH_mm_ss"));
string fullPath = Path.Combine(path, fileName);
Not sure if that solves your issue, but it increases readability and avoids careless mistakes anyway.
You don't write what error you get. But here are some hints about how you can simplify your code:
var dir = HttpContext.Current.Server.MapPath(
ConfigurationManager.AppSettings["LogFileFolder"].ToString());
var dt = DateTime.Now.ToString("yyyy.MM.dd_HH.mm.ss");
var logFilePath = Path.Combine(dir, string.Format("Log_{0}.txt", dt));
var traceFilePath = Path.Combine(dir, string.Format("Scheduler_{0}.txt", dt));
var fs = File.Create(logFilePath);
var fs1 = File.Create(traceFilePath);
Notes:
if the app-settings entry LogFileFolder already contains an (absolute) filesystem-path such as c:\temp, then you shouldn't call Server.MapPath().
you should call fs.Close() once you no longer need the streams (or put it in a using block). Otherwise, another attempt to create the (same) file will result in an exception.
i have to fetch those records where cityname like'zipcode' where zipcode is variable and apply conditions
var zipcd = (from u in db.ZipCodes1
where u.CityName.Contains(zipcode) && u.CityType == "D"
select u).ToList().Select(u => new Viewsearch
{
Zipcode = u.ZIPCode,
CityName = u.CityName,
stateabbr = u.StateAbbr
}).Distinct();
Viewsearch vs = (Viewsearch)zipcd;
if (zipcd.Count() > 1)
{
locations = "United States;" + vs.stateabbr + ";" + vs.CityName;
}
else if (locations == "")
{
locations = "United States;" + vs.stateabbr + ";" + vs.CityName;
}
else
{
locations = "United States;" + vs.stateabbr + ";" + vs.CityName + "," + locations;
}
if (zipcd.Count() > 3) is greater than 3
{
locations = locations.Replace(locations, "," + "<br>");
}
The problem is that you're casting an iterator to the type of a single element on the line
ViewSearch vs = (ViewSearch)zipcd.
If you want vs to be a single object, you must call First() or FirstOrDefault() on your collection:
ViewSearch vs = zipcd.First(); // Throws if there are no elements
ViewSearch vs = zipcd.FirstOrDefault(); // null if there are no elements
First of all I would suggest that you download and use the lovely LINQPad not only to run your LINQ queries first but also to learn from it (has a lot of samples that you can run right form there, no more config needed)
for your question:
var zipcd = (
from u in db.ZipCodes1
where u.CityName.Contains(zipcode) && u.CityType == "D"
select new Viewsearch
{
Zipcode = u.ZIPCode,
CityName = u.CityName,
stateabbr = u.StateAbbr
}).Distinct().ToList();
As you can see the query works:
Distinct at the end of your query uses IEqualityComparer, and I'm guessing you haven't defined one for Viewsearch. It would look something like this:
public class ViewsearchComparer : IEqualityComparer<Viewsearch>
{
public bool Equals(Viewsearch vs1, Viewsearch vs2)
{
// Implementation
}
public int GetHashCode(Viewsearch vs)
{
// Implementation
}
}
After you have that defined, you pass it into your distinct call:
.Select(u => new Viewsearch
{
Zipcode = u.ZIPCode,
CityName = u.CityName,
Stateabbr = u.StateAbbr
})
.Distinct(new ViewsearchComparer());