Eiffel: unknown identifier `cursor` on LINKED_LIST[STRING] - eiffel

I have an unknown identifier on the local like some_values.cursor, I really don't understand why!
qry_update_set_fields (some_keys, some_values, some_unstored_field_names: LINKED_LIST[STRING]): STRING
require
same_some_keys_some_values_count: some_keys.count = some_values.count
local
l_val_c: like some_values.new_cursor
do
Result := ""
l_val_c := some_values.new_cursor
across
some_keys as l_key_c
loop
Result := l_key_c.item + "=" + l_val_c.item + ","
l_val_c.forth
end
if Result.ends_with (",") then
Result.remove_tail (1)
end
ensure
dont_modify_parameters: old some_keys.is_equal (some_keys) and old some_values.is_equal (some_values)
end
working
l_val_c: LINKED_LIST_CURSOR [STRING]
Neither working
l_val_c: LINKED_LIST_CURSOR [like some_values.item]

The example tries using an argument in the qualified anchored type like argument.some_feature. This is not supported. Types, anchored to arguments, are not part of the standard Eiffel and are supported only for backward compatibility in the form like argument.

Related

Eiffel: regular expressions how to do grouping

I'd like to do grouping of regular expressions with eiffel. How do I do something like
l_reg.compile ("^([0-9]{3}) (rabbit[0-9]).*")
l_groups := l_reg.groups ("123 rabbit1")
my_first_rabbit := l_groups.at (2)
Didn't find any example on groups, LX_DFA_REGULAR_EXPRESSION class and other googlings
One solution is to use RX_PCRE_REGULAR_EXPRESSION instead of LX_DFA_REGULAR_EXPRESSION:
Including $ISE_LIBRARY\contrib\library\gobo\library\regexp\src\library.ecf library
l_reg: RX_PCRE_REGULAR_EXPRESSION
...
l_reg.compile ("^([0-9]{3}) (rabbit[0-9]).*")
l_reg.match ("123 rabbit1")
my_first_rabbit := l_reg.captured_substring (2)
There is no groups routine, although it could be implemented by calling captured_substring internally. There is only a routine split which does the reverse: returns the substrings which did not match the regular expression.
Something like
regex_groups (a_haystack, a_needle: STRING): ARRAY[STRING]
-- Test with https://www.regextester.com/1911
require
regex_match (a_haystack, a_needle)
local
l_reg: RX_PCRE_REGULAR_EXPRESSION
l_index: like {RX_PCRE_REGULAR_EXPRESSION}.match_count
do
create Result.make_empty
create l_reg.make
l_reg.compile (a_needle)
if l_reg.is_compiled then
l_reg.match (a_haystack)
from
l_index := 1
until
l_index > l_reg.match_count
loop
Result.extend (l_reg.captured_substring (l_index))
l_index := l_index + 1
end
else
--- error compiling regex
fallible.set_last_error (create {SIT_INTERNAL_ERROR}.make ("regex_group-> regex does not compile:" + a_needle))
end
ensure
not fallible.has_error
instance_free: Class
end
you could test your regex here: https://www.regextester.com/1911

How can I get a file's group ID (GID) in Go?

os.Stat() returns a FileInfo object, which has a Sys() method that returns an Interface{} with no methods.
Though I am able to fmt.Printf() it to "see" the "Gid", I am unable to access "Gid" programmatically.
How do I retrieve the "Gid" of a file here?
file_info, _ := os.Stat(abspath)
file_sys := file_info.Sys()
fmt.Printf("File Sys() is: %+v", file_sys)
Prints:
File Sys() is: &{Dev:31 Ino:5031364 Nlink:1 Mode:33060 Uid:1616 Gid:31 X__pad0:0 Rdev:0 Size:32 Blksize:32768 Blocks:0 Atim:{Sec:1564005258 Nsec:862700000} Mtim:{Sec:1563993023 Nsec:892256000} Ctim:{Sec:1563993023 Nsec:893251000} X__unused:[0 0 0]}
Note: I do not need a portable solution, it just has to work on Linux (notable because Sys() is known to be flaky).
Possibly related: Convert interface{} to map in Golang
The reflect module showed that the data type for Sys()'s return is *syscall.Stat_t, so this seems to work to get the Gid of a file as a string:
file_info, _ := os.Stat(abspath)
file_sys := file_info.Sys()
file_gid := fmt.Sprint(file_sys.(*syscall.Stat_t).Gid)
Please let me know if there is a better way to do this.

How to create a for loop when the variable is in a sentence?

I want to create a for loop when the variable is in a string format.
my code is:
for i in range(1,32):
DEP_RATE = pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = i:"{i}" group by YYYYMM',{i:str(i)},rconn)
the error is:
'dict' object has no attribute 'cursor'
I used this code:
for i in range(1,32):
DEP_RATE = pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = "i" group by YYYYMM',rconn, params={i:str(i)})
It hasn't error but doesn't work. the problem is:
("Truncated incorrect DOUBLE value: 'i'")
This appears to be a PANDAS question, yes? I believe it's an argument mismatch with the read_sql_query API. The function signature is:
pandas.read_sql_query(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, chunksize=None)
Which roughly says that the first two positional arguments are required, and everything else is defaulted or optional.
To make it more obvious, consider putting your core SQL string on a separate line:
sql = 'SELECT daynum, ... AND daynum = :i'
for i in range(1, 32):
DEP_RATE = pd.read_sql_query(sql, {i: str(i)}, rconn)
With this formulation, it's more obvious that your arguments do not match the API: your second argument is off. Perhaps you want (note the additional keyword argument params):
DEP_RATE = pd.read_sql_query(sql, rconn, params={i: str(i)})
I corrected the code. The correct answer is:
DEP_RATE = list(np.zeros((1,1)))*31
for i in range(1,32):
DEP_RATE[i-1] =np.array(pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = '+str(i)+' group by YYYYMM;',rconn, params={i:str(i)}))

Splitting String into Array Errors

Trying to write a script that will be run in WinPE, that essentially gets the IP address of the localhost and chooses an action based on the IP range.
In Windows, the script runs flawlessly. However, in WinPE, I'm getting the following error:
script.vbs(1,1) Microsoft VBScript runtime error: Subscript out of range
Google-fu is telling me that has something to do with my array being outside of the range. Here I thought I had a decent understanding, but apparently not.
Code that works as is on Windows:
Option Explicit
Dim sIP, sHostname,sPingBat
Dim aIP
Dim iOct1, iOct2, iOct3, iOct4, iReturn
Dim oWMIService, oCmd, oAdapter
Dim cAdapters
iReturn = 999
sHostname = "."
Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sHostname & "\root\cimv2")
Set cAdapters = oWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set oCmd = CreateObject("Wscript.Shell")
For Each oAdapter in cAdapters
If Not IsNull(oAdapter.IPAddress) Then
sIP = Trim(oAdapter.IPAddress(0))
Else
iReturn = 404
WScript.Quit iReturn
End If
Next
sIP = CStr(sIP)
aIP = Split(sIP, ".")
iOct1 = CInt(aIP(0))
iOct2 = CInt(aIP(1))
iOct3 = CInt(aIP(2))
iOct4 = CInt(aIP(3))
Now, if I change the declaration of the aIP array to either of the following:
aIP()
aIP(4)
and run
aIP = Split(sIP, ".")
I get
script.vbs(26, 1) Microsoft VBScript runtime error: Type mismatch
Changing the array assignment / split line to
aIP() = Split(sIP,".")
results in
script.vbs(26, 1) Microsoft VBScript runtime error: Subscript out of range
So I'm obviously doing something wrong.
It's also entirely possible that my original error message is completely unrelated to my array range, and WinPE just doesn't like my script (in which case, if anybody has any pointers, it'd be appreciated)
At the moment, I'm mounting my wim to get the install packages to make sure the WMI and Scripting packages are installed from the ADK.
There is nothing wrong with the code except the assumption being made about what Win32_NetworkAdapterConfiguration returns.
From MSDN - Win32_NetworkAdapterConfiguration class
Array of all of the IP addresses associated with the current network adapter. This property can contain either IPv6 addresses or IPv4 addresses. For more information, see IPv6 and IPv4 Support in WMI.
Because sIP could contain an IPv6 address the Split() will not work as expected. IPv6 addresses don't contain . as a separator so Split() will return a Array containing the original string as the first index only. Hence attempting to read anything other then aIP(0) will cause an
Microsoft VBScript runtime error:
Subscript out of range
error.
To avoid this use InStr() to check for the existence of . in the sIP variable first, you will also need to iterate through the oAdapter.IPAddress array to check each address to get the correct one, you can't assume IPAddress(0) will always be the right one.
Try this
Dim ips, ip
For Each oAdapter in cAdapters
ips = oAdapter.IPAddress
If IsArray(ips) Then
For Each ip In ips
If InStr(1, ip, ".") > 0 Then
sIP = Trim(ip)
Exit For
End If
Next
If Len(sIP) > 0 Then Exit For
Else
iReturn = 404
WScript.Quit iReturn
End If
Next
Untested on iPad sorry
I guess sIP variable contains some string which can not be splitted wity delimiter "."(ex: "somestringwithNoDOT")
So in the 1st case
aIP = Split(sIP,".") ' Split("somestringwithNoDOT",".")
statement returned only 1 string, which can not be coverted to Integer. So i returned Type mismatch error in below line
iOct1 = CInt(aIP(0)) ' returns Type mismatch error
In the 2nd case
aIP() = Split(sIP,".") ' Split("somestringwithNoDOT",".")
above statement will return 1 element, but aIP is array with NO elements. So this statement rturned "Subscript out of range" error
Resolution for this issue is to check whether correct value is passing to sIP

Compact C Folding in Vim

I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:
static void funca(...)
{
...
}
/* Example comment */
static void funcb(...)
{
...
}
Then it would create folds which would look like this when closed:
+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------
So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.
I know how to make one of these folds (assuming foldmethod=manual):
/^{<cr>kVnn?^$<cr>zf
But I'm not sure how to put it into a function. This is my effort:
function Cfold()
set foldmethod=manual " Manual folds
ggzE " Delete all folds
while (/^{<cr>) " Somehow loop through each match
kVnn?^$<cr>zf " This would work fine except for the last function
endwhile
endfunction
map <Leader>f :call Cfold()<cr>
But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.
Thanks in advance :)
You can create folds programmatically using the foldexpr and foldtext. Try this, though you may have to tweak CFoldLevel so it doesn't swallow non-function parts of the code:
function! CFoldLevel(lnum)
let line = getline(a:lnum)
if line =~ '^/\*'
return '>1' " A new fold of level 1 starts here.
else
return '1' " This line has a foldlevel of 1.
endif
endfunction
function! CFoldText()
" Look through all of the folded text for the function signature.
let signature = ''
let i = v:foldstart
while signature == '' && i < v:foldend
let line = getline(i)
if line =~ '\w\+(.*)$'
let signature = line
endif
let i = i + 1
endwhile
" Return what the fold should show when folded.
return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' '
endfunction
function! CFold()
set foldenable
set foldlevel=0
set foldmethod=expr
set foldexpr=CFoldLevel(v:lnum)
set foldtext=CFoldText()
set foldnestmax=1
endfunction
See :help 'foldexpr' for more details.

Resources