How to use regex to match a full string in Python. How to print the full value of a long string variable when debugging Go code with Delve. How to configure string length limits in various debugging environments.
Understanding Regex for Full String Matching
When working with regular expressions, it’s crucial to understand how to match an entire string. Let’s explore this concept using Python as an example.
The Problem with Partial Matches
Consider the following Python code:
“`python
import re
class Foo():
_rex = re.compile(“\d+”)
def bar(self, string):
m = _rex.match(string)
if m != None:
doStuff()
“`
This code attempts to check if a string is a number using the regex pattern “\d+”. However, this pattern can lead to unexpected results. For instance, it would match “78.46.92.168:8000”, which is not a simple number.
Why Does This Happen?
The regex pattern “\d+” matches one or more digits anywhere in the string. It doesn’t require the entire string to be composed of digits. In the case of “78.46.92.168:8000”, it matches the first group of digits “78” and considers it a successful match.
Solution: Matching the Entire String
To match the entire string, we need to use anchors. Here’s an improved regex pattern:
“`python
_rex = re.compile(“^\d+$”)
“`
In this pattern:
- ^ asserts the start of the string
- \d+ matches one or more digits
- $ asserts the end of the string
This ensures that the entire string consists of digits from start to end.
Debugging Large Strings in Go with Delve
When debugging Go code, you might encounter situations where you need to inspect large string variables. The Delve debugger provides ways to handle this.
Default Behavior of Delve
By default, when you try to print a long string in Delve, it truncates the output. For example:
“`
(dlv) print myString
“my string…+539 more”
“`
Configuring Delve for Full String Output
To print the full string, you can configure the `max-string-len` option in Delve:
“`
(dlv) config max-string-len 1000
“`
This sets the maximum string length to 1000 characters. After applying this configuration, running `print myString` should display the entire string.
Persistent Configuration for Delve
To avoid setting the configuration each time you run Delve, you can create a persistent configuration file.
Location of Delve Configuration File
The Delve configuration file is typically located at:
- MacOS and Linux: $HOME/.dlv/config.yml
- Linux with $XDG_CONFIG_HOME set: $XDG_CONFIG_HOME/dlv/config.yml
Sample Configuration
Add the following to your config.yml file:
“`yaml
max-array-values: 1000
max-string-len: 1000
“`
Debugging Go in Visual Studio Code
Visual Studio Code offers its own debugging features for Go, which can be configured for better handling of large strings.
VSCode Settings for Go Debugging
Add the following configuration to your VSCode settings:
“`json
“go.delveConfig”: {
“dlvLoadConfig”: {
“maxStringLen”: 1024,
},
“apiVersion”: 2,
}
“`
Recent Changes in VSCode Go Debugger
It’s important to note that the `dlvLoadConfig` with `maxStringLen` has been deprecated in recent versions of the VSCode Go debugger. The new debug adapter takes a different approach with on-demand loading of composite data and updated string limits.
Alternative Methods for Viewing Large Strings in VSCode
If you’re using the new VSCode Go debugger and need to view large strings, you have several options:
- Hover over the variable in the source code
- Use “Copy as Expression” to query the string via REPL in the DEBUG CONSOLE panel
- Use “Copy Value” to copy the entire string to clipboard
These methods allow you to access a larger limit of 4096 characters, which should be sufficient for most use cases.
Best Practices for Handling Large Strings in Debugging
When working with large strings during debugging, consider the following best practices:
- Use appropriate configuration settings to balance between readability and performance
- Leverage built-in tools and features of your debugging environment
- Consider breaking down large strings into smaller, manageable chunks for analysis
- Use logging or custom print statements for very large strings that exceed debugger limits
Troubleshooting Common Issues with String Debugging
Even with proper configuration, you might encounter issues when debugging large strings. Here are some common problems and their solutions:
Debugger Slowdown
If your debugger becomes slow when handling large strings, try reducing the `max-string-len` value. Find a balance between the amount of data you need to see and the debugger’s performance.
Inconsistent Behavior Across Environments
Debugging behavior might vary across different environments (e.g., local machine vs. CI/CD pipeline). Ensure that your Delve configuration is consistent across all environments where you debug your code.
Memory Issues
Loading very large strings into memory during debugging can cause memory issues. If you’re dealing with extremely large strings, consider implementing custom logging or serialization methods to inspect the data without loading it entirely into memory.
Advanced Techniques for String Analysis in Go
Beyond simple printing, there are advanced techniques you can use to analyze large strings in Go:
Using String Slices
Instead of loading the entire string, you can work with slices of the string:
“`go
fmt.Println(myLargeString[:100]) // Print first 100 characters
fmt.Println(myLargeString[len(myLargeString)-100:]) // Print last 100 characters
“`
Implementing Custom Inspection Functions
You can create custom functions to analyze specific parts of your string:
“`go
func inspectString(s string) string {
if len(s) > 1000 {
return fmt.Sprintf(“Length: %d, First 100: %s…, Last 100: …%s”,
len(s), s[:100], s[len(s)-100:])
}
return s
}
“`
Using Regular Expressions for Analysis
Regular expressions can be powerful tools for analyzing large strings:
“`go
import “regexp”
func countOccurrences(s, substr string) int {
re := regexp.MustCompile(regexp.QuoteMeta(substr))
return len(re.FindAllString(s, -1))
}
“`
This function counts the occurrences of a substring within a larger string, which can be useful for analyzing patterns in large text data.
Optimizing Go Code for Large String Handling
When working with large strings in Go, it’s important to optimize your code for efficiency. Here are some techniques:
Using Bytes Instead of Strings
For very large strings, working with byte slices can be more efficient:
“`go
data := []byte(largeString)
// Perform operations on data
“`
Implementing io.Reader Interface
For streaming large strings, implement the io.Reader interface:
“`go
type StringReader struct {
s string
i int64
}
func (r *StringReader) Read(p []byte) (n int, err error) {
if r.i >= int64(len(r.s)) {
return 0, io.EOF
}
n = copy(p, r.s[r.i:])
r.i += int64(n)
return
}
“`
This allows you to process the string in chunks, which is memory-efficient for very large strings.
Using StringBuilder for Concatenation
When building large strings, use strings.Builder for efficient concatenation:
“`go
var builder strings.Builder
for i := 0; i < 1000; i++ {
builder.WriteString("some text")
}
result := builder.String()
```
This is more efficient than using the + operator for multiple concatenations.
Security Considerations in String Handling
When working with large strings, especially in debugging environments, it’s crucial to consider security implications:
Sanitizing Sensitive Data
Be cautious when printing or logging large strings that might contain sensitive information. Implement sanitization functions:
“`go
func sanitizeString(s string) string {
// Replace sensitive patterns
s = regexp.MustCompile(`\d{4}-\d{4}-\d{4}-\d{4}`).ReplaceAllString(s, “XXXX-XXXX-XXXX-XXXX”)
return s
}
“`
Limiting String Exposure
In production environments, limit the exposure of large strings:
“`go
func safelyPrintString(s string) {
if len(s) > 1000 {
fmt.Printf(“String too large to display (length: %d)\n”, len(s))
} else {
fmt.Println(s)
}
}
“`
Secure Storage of Debug Information
When storing debug information containing large strings, ensure it’s stored securely and access is properly restricted.
By implementing these advanced techniques and considering security aspects, you can effectively work with large strings in Go while maintaining code efficiency and data security. Remember to always balance between debugging needs and potential security risks when handling sensitive data in strings.
python – Checking whole string with a regex
Asked
Modified
3 years, 6 months ago
Viewed
42k times
I’m trying to check if a string is a number, so the regex “\d+” seemed good. However that regex also fits “78.46.92.168:8000” for some reason, which I do not want, a little bit of code:
class Foo(): _rex = re.compile("\d+") def bar(self, string): m = _rex.match(string) if m != None: doStuff()
And doStuff() is called when the ip adress is entered. I’m kind of confused, how does “.” or “:” match “\d”?
- python
- regex
\d+
matches any positive number of digits within your string, so it matches the first 78
and succeeds. \d+$
0
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Post as a guest
Email
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.
debugging – How do I print the full value of a string variable in delve?
Asked
Modified
7 months ago
Viewed
17k times
I’m using the delve go debugger to debug some code. When I try to print a string variable, it gives me an abbreviated version.
(dlv) print myString "my string...+539 more"
How do I get it to print the full string?
- debugging
- go
- delve
The ability to configure the length of printed strings was recently added to delve. To see the full list of configuration options, run config -list
;
(dlv) config -list aliases map[] substitute-path [] max-string-len <not defined> max-array-values <not defined> show-location-expr false
The one we’re interested in here is called max-string-len
, which you can see is currently <not defined>
. To increase the length to e.g. 1000, run
(dlv) config max-string-len 1000
Now running print myString
should print the whole string.
Just to add to your answer, if you’re using VS Code debugging feature, add the following config to your settings. json
:
"go.delveConfig": { "dlvLoadConfig": { "maxStringLen": 1024, }, "apiVersion": 2, },
Adding to the above answers, to have these configs applied each time you run dlv
, you should be able to find the config file in (see the sourcecode):
$HOME/.dlv/config.yml
by default on MacOS$HOME/.dlv/config.yml
by default on Linux. If$XDG_CONFIG_HOME
is set then it should be in$XDG_CONFIG_HOME/dlv/config.yml
For example, the relevant region in the config.yml
file:
... # Maximum number of elements loaded from an array. max-array-values: 1000 # Maximum loaded string length. max-string-len: 1000 ...
Type 'help' for list of commands. (dlv) config -list ... max-string-len 1000 max-array-values 1000 ...
1
VSCode’s Go Debugger
I need to view large strings.
How can I do that if
dlvLoadConfig
withmaxStringLen
is deprecated?The legacy adapter used
dlvLoadConfig
as one-time session-wide
setting to override dlv’s conservative default variable loading
limits, intended to protect tool’s performance. The new debug adapter
is taking a different approach with on-demand loading of composite
data and updated string limits, relaxed when interacting with
individual strings. In particular, if the new default limit of 512,
applied to all string values in the variables pane, is not sufficient,
you can take advantage of a larger limit of 4096 with one of the
following:
- Hover over the variable in the source code
Copy as Expression
to query the string via REPL in the DEBUG CONSOLE panelCopy Value
to clipboardPlease open an issue
if this is not sufficient for your use case or if you have any
additional feedback.
Source
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Post as a guest
Email
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.
To transfer Dr.Web Server (when installing a similar version of Dr.Web Server) under Windows OS: 1.Stop the Dr.Web Server service. 2.Run the drwcsd.exe file with the exportdb key from the command line to export the contents of the database to a file. The full command line for exporting to the Windows version will look something like this:
3.Save the contents of the C:\Program Files\DrWeb Server\etc directory, as well as the drwcsd.pub key from C:\Program Files\DrWeb Server\Installer. 4.Delete the Server. 5.Install a new Server (empty, with a new database) on the required computer. Stop the Dr.Web Server service using the Windows OS service management tools or the Control Center. 6.Copy the contents of the previously saved etc directory to C:\Program Files\DrWeb Server\etc, as well as the drwcsd.pub key to C:\Program Files\DrWeb Server\Installer. 7.Run the drwcsd.exe file from the command line with the importdb key to import the contents of the database from the file. The full import command line for the Windows version will look something like this:
8.Start the Dr.Web Server service.
To migrate Dr.Web Server (when installing another version of Dr.Web Server) under Windows OS: 1.Stop the Dr.Web Server service. 2.Save the database using the SQL server (if using the built-in database, just save the database.sqlite file). 3.Save the contents of the C:\Program Files\DrWeb Server\etc directory, as well as the drwcsd.pub key from C:\Program Files\DrWeb Server\Installer. 4.Delete the Server. 5.Install a new Server (empty, with a new database) on the required computer. Stop the Dr.Web Server service using the Windows OS service management tools or the Control Center. 6.Copy the contents of the previously saved etc directory to C:\Program Files\DrWeb Server\etc, as well as the drwcsd.pub key to C:\Program Files\DrWeb Server\Installer. 7. Restore the database on the new Server, specify the path to the database in the drwcsd.conf configuration file. 8.Run the drwcsd.exe file with the upgradedb key from the command line to upgrade the database. The full command line for import in the Windows version will look something like this:
9.Start the Dr.Web Server service. In case of name or IP address change during Dr.Web Server transfer:
1.Perform the Server migration according to the relevant procedure described above. 2.For all Agents served by the old Server, set the address of the new Server according to the corresponding procedure from the section Connecting Dr. Web Agent to another Dr.Web Server. For Agents for which the address of the new Server was specified via the Control Center, and not in the settings of the Agent itself on the station, the address of the new Server must be specified in the Agent settings on both Servers. 3.Wait until all Agents are transferred to the new Server. After that, you can delete the old Server. |
How to improve typography in InDesign
How a page of text looks like, whether it’s a printed edition or an article on a website, determines whether someone will read it or not. Typography in layout is not only aesthetics, but also an attention management tool. I have identified six basic layout principles that will help make layouts more confident and readable:
- Always mark paragraphs in your text.
- Make the line length optimal for reading.
- Choose the right alignment.
- Type body text in lowercase.
- Monitor lowercase height using different fonts.
- Enable optical margin alignment.
Always mark paragraphs in the text
Let’s take the canvas of text:
Although not noticeable, there are paragraphs. I will include hidden characters in InDesign for clarity:
In this form, the text is difficult to perceive and read. Paragraphs should be clearly separated. There are two ways to do this – “red line” and spacing.
Let’s add a “red line”:
Already better. But it should be remembered that in the very first paragraph it is not necessary to indent, there is nothing to separate it from. It would be correct to do this:
Add a spacer:
In this form, paragraphs are even better read. But it is not necessary to use both methods at the same time – this is overkill. Let’s remove the “red line”:
Great now.
You can use both the “red line” and the spacer together if you need to separate the introductory paragraph from the main body of the text. It will look like this:
Make the line length optimal for reading
It is difficult to read text when the line is too long and the font size is quite small. In the example below, we see that there are ~120 characters per line:
If you increase the font size for the same line length, the text becomes easier to read. Here there are ~9 per line0 characters:
If, on the contrary, the line length is reduced, the text is hard to read. This can be justified when there is little text, for example, in a marginal footnote, but not in typesetting. The eyes jump from line to line too often – this is quite stressful:
I recommend keeping a balance – focus on 45-90 characters per line:
In this form, the text is convenient to read, the line is not lost and the eyes do not get tired of constant jumps.
Choose the right justification
Justification or justification can be left, right, fit, center and full:
Left justification and format are commonly used for typesetting. I love the left justification. It seems to me the most comfortable and aesthetically pleasing.
Center justified can be found in headings, quotations or footnotes.
Right-justified is also used in headings, quotations, or footnotes, and rarely for typesetting.
Complete shutdown is rarely used. I met her in PTYUCH magazine. In general, in this magazine it is quite difficult to find at least something similar to an adequate layout, but this is the whole PTYUCH:
Issues No. 9 of 1996, Issues No. 2 and No. 5 of 1998
I also noticed that the Russian edition of Vogue also likes full flush, especially often used in headlines:
I recommend not to use full flush at all, except to achieve what something special effect.
I’ll tell you more about right justification and format – these two options seem to me the most difficult to use.
Justification on the right edge
By right-aligning the text, you can forget about the “red lines” – they are simply not visible due to the torn edge:
should be used:
and it is especially important to balance the lines, for this you need to turn on the Balance Ragged Lines box:
Without this checkmark, the line vary strongly and hanging pretexts appear:
It looks like this, if you increase the length of the line:
And here I have balanced the lines again:
Format justification
Reminds me of a “brick”, so stable and serious:
Two main problems can arise with this formatting: holes in lines and overly compressed lines:
I will turn on the highlighting of problem areas in the text in InDesign, this is done in the settings:
Almost everything glows yellow – this means that the formatting is unsuccessful. Now in paragraph styles, the maximum and minimum value of the Glyph Scaling parameter is 100%:
By changing these parameters, you can achieve a readable text design:
As you can see in the image above, the yellow highlight is gone. According to Indesign, everything is fine now, although it is still not ideal for me.
You must also use hyphenation in format alignment. If you turn them off, the words in some lines will completely stick together:
You can try to fix the situation in the Glyph Scaling parameter, but then holes appear:
In general, everything is not so simple with this type of alignment.
Type body text in lowercase
When you type in uppercase letters, you create a “scream” that is impossible to read:
You should also avoid uppercase letters if you want to emphasize a word or phrase within the text:
It is better to use a style two steps apart from the body text. For example, if your body text is set to Light, you can use SemiBold to highlight it:
Watch your lowercase height using different fonts
Let’s say you need to use two different fonts in one text – one with serifs, the other without.